2013-07-04 50 views
-1

我在我的片段中显示ImageView以在图像之间滑动。感谢this后我可以调整图像和imageView到原始图像的宽度和高度。但是,如果图像始终位于左上角。然后我改变了片段的布局,试图将imageView水平和垂直居中。但是随着的RelativeLayout或LinearLayout中围绕着它后,我得到了评论的IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.更改布局导致illegalStateException

不工作代码:

片段:

ImageView image; 
Bitmap bitmap = null; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) 
{ 
    image = (ImageView) (ImageView) inflater.inflate(R.layout.fragment_image_page, container, false); 
    // ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_image_page, container, false); 
    // image = (ImageView) rootView.findViewById(R.id.imageView); 
    new ImageLoad().execute(""); 
    return image; 
} 


private class ImageLoad extends AsyncTask<String, Void, Boolean> 
{ 
    int width; 
    int height; 
    @Override 
    protected Boolean doInBackground(String... params) 
    { 
     URL url; 
     try 
     { 
      url = new URL(
        "http://www.....jpg"); 
      // !!! ERROR IN FOLLOWING LINE !!! 
      bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
      width = bitmap.getWidth(); 
      height = bitmap.getHeight(); 
      int bounding = getResources().getDisplayMetrics().widthPixels; 

      if (bounding < width) 
      { 
       float ratio = width/height; 
       width = bounding; 
       height = (int) (width/ratio); 
       bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height); 
      } 
     } catch (MalformedURLException e) 
     { 
      e.printStackTrace(); 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(Boolean doInBackground) 
    { 
     FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) image.getLayoutParams(); 
     param.width = width; 
     param.height = height; 
     image.setLayoutParams(param); 
     image.setImageBitmap(bitmap); 
    } 
} 

XML工作:

<?xml version="1.0" encoding="utf-8"?> 
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:contentDescription="BILD" /> 

XML不working:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerInParent="true" > 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="BILD" /> 

</RelativeLayout> 

回答

0

改变XML文件后,必须返回根充气view.Which是你的ViewGroup。

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) 
    { 

     ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_image_page, container, false); 
     image = (ImageView) rootView.findViewById(R.id.imageView); 
     new ImageLoad().execute(""); 
     return rootView; 
    } 
+0

对不起,这个愚蠢,愚蠢的错误*耻辱* – lis

0

onCreateView()中,供应null而不是container。你也不需要投两次ImageView

像这样:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    image = (ImageView) inflater.inflate(R.layout.fragment_image_page, null, false); 
    ... 
    return image; 
}