动态图像视图。指定的 child 已经有 parent

Dynamic imageView. Specified child already has parent

我正在尝试添加动态图像。

这是我遇到的错误

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我也加

  ((ViewGroup)imageView.getParent()).removeView(imageView);

但没有解决我的问题

    @Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == ConstantsCustomGallery.REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {

                ArrayList<Image> images = data.getParcelableArrayListExtra(ConstantsCustomGallery.INTENT_EXTRA_IMAGES);


                for (int i = 0; i < images.size(); i++) {
                    filePath = Uri.fromFile(new File(images.get(i).path));
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200, 200);
                    imageView.setLayoutParams(lp);
                    ((ViewGroup)imageView.getParent()).removeView(imageView);
                    Glide.with(MainActivity.this).load(filePath).override(200,200).crossFade().into(imageView);
                    linearLayout.addView(imageView);


                }
                  buttonConfirm.setVisibility(View.VISIBLE);

                progressDialog.setProgress(0);

            }
}

这里是XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#f4f7f9"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:weightSum="1">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Logout"
                android:textSize="22sp"
                android:textColor="#DF0713"
                android:id="@+id/logout_text"
                android:layout_toLeftOf="@+id/logout"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/logout"
         android:layout_alignParentEnd="true"
            android:background="@drawable/logout"/>
        </RelativeLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"

            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:hint="Image Title"
                android:inputType="textEmailAddress"
                android:textColor="@color/colorPrimaryDark"
                android:textColorHint="@color/colorPrimaryDark" />
        </android.support.design.widget.TextInputLayout>


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_marginTop="10dp" />


    </LinearLayout>





    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_marginBottom="17dp"
        android:layout_marginEnd="17dp"
        android:clickable="true"
        app:fabSize="normal"
android:layout_below="@+id/linearLayout"
        app:backgroundTint="#DF0713"
        app:srcCompat="@drawable/splash"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        app:borderWidth="0dp"
        app:elevation="0dp"
        />
    </RelativeLayout>
</ScrollView>

问题是您一次又一次地添加相同的图像视图,这是不可能的。您需要创建新的 ImageVIew 并将其添加到任何您想要的地方

for (int i = 0; i < images.size(); i++) {
    filePath = Uri.fromFile(new File(images.get(i).path));
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200, 200);
    ImageView iv = new ImageView(context); // context mean YourActivity.this or context object
    iv.setLayoutParams(lp);
    //((ViewGroup)imageView.getParent()).removeView(imageView);
    Glide.with(MainActivity.this).load(filePath).override(200,200).crossFade().into(iv);
    linearLayout.addView(iv);
}