Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更改布局导致非法状态异常_Android_Imageview - Fatal编程技术网

Android 更改布局导致非法状态异常

Android 更改布局导致非法状态异常,android,imageview,Android,Imageview,我在片段中显示ImageView,以便在图像之间滑动。多亏了post,我可以根据原始图像的宽度和高度调整图像和图像视图。但是如果图像总是在左上角。然后,我改变了片段的布局,尝试将imageView水平和垂直居中。但是在使用RelativeLayout或LinearLayout将其包围之后,我得到了一个IllegalStateException:指定的子级已经有了父级。必须首先对孩子的家长调用removeView()。 注释中的非工作代码: 片段: ImageView image; Bitmap

我在片段中显示ImageView,以便在图像之间滑动。多亏了post,我可以根据原始图像的宽度和高度调整图像和图像视图。但是如果图像总是在左上角。然后,我改变了片段的布局,尝试将imageView水平和垂直居中。但是在使用RelativeLayout或LinearLayout将其包围之后,我得到了一个
IllegalStateException:指定的子级已经有了父级。必须首先对孩子的家长调用removeView()。

注释中的非工作代码:

片段:

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);
    }
}
ImageView图像;
位图=空;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState)
{
图像=(ImageView)(ImageView)充气器。充气(R.layout.fragment\u image\u页面,容器,false);
//ViewGroup rootView=(ViewGroup)充气器。充气(R.layout.fragment\u image\u页面,容器,false);
//image=(ImageView)rootView.findviewbyd(R.id.ImageView);
新建ImageLoad()。执行(“”);
返回图像;
}
私有类ImageLoad扩展异步任务
{
整数宽度;
内部高度;
@凌驾
受保护的布尔doInBackground(字符串…参数)
{
网址;
尝试
{
url=新url(
"http://www.....jpg");
//!!!以下行有错误!!!
位图=BitmapFactory.decodeStream(url.openConnection().getInputStream());
宽度=位图.getWidth();
高度=位图.getHeight();
int bounding=getResources().getDisplayMetrics().widthPixels;
如果(边界<宽度)
{
浮动比率=宽度/高度;
宽度=边界;
高度=(int)(宽度/比率);
位图=位图。创建位图(位图,0,0,宽度,高度);
}
}捕获(格式错误)
{
e、 printStackTrace();
}捕获(IOE异常)
{
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(布尔doInBackground)
{
FrameLayout.LayoutParams param=(FrameLayout.LayoutParams)image.getLayoutParams();
参数宽度=宽度;
参数高度=高度;
image.setLayoutParams(param);
setImageBitmap(位图);
}
}
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 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>

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 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>

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;
}

更改xml文件后,必须返回根视图。它是您的视图组

   @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;
    }