2

我正在嘗試添加動態圖像。動態imageView。指定的孩子已經有父母

這是錯誤我面對

產生的原因:java.lang.IllegalStateException:指定的子 已經有母。您必須先調用孩子父母 上的removeView()。

我還加

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

回答

2

問題是要添加相同的圖像視圖一次又一次,這是不可能的。您需要創建新的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); 
} 
相關問題