1

I created AlertDialog with setting my custom view, everything looks good when I click to show it for first time, but on second time I got java.lang.IllegalStateException

the XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/linearLayout"
    >

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/logo04"
        android:layout_gravity="center"
        />

    <!--<view style="@style/Divider" />-->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="15dp"
        android:text="@string/app_name"
        android:textSize="20sp"
        android:textColor="@color/black"
        />


    <!--<view style="@style/Divider" />-->

    <TextView
        android:id="@+id/versionCode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:textColor="@color/black"
        android:textSize="20sp"
        />

    <!--<view style="@style/Divider" />-->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/contactUs"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:layout_margin="15dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/contactEmail"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:layout_margin="15dp"
        android:autoLink="email"
        />

</LinearLayout>

Inflating layout under onCreate method

viewGroup = findViewById(R.id.linearLayout);
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.about, viewGroup);

and here's on onNavigationItemSelected the AlertDialog using

               case R.id.about:
                if(viewGroup != null){
                    viewGroup.removeAllViews();
                }
                AlertDialog.Builder aboutDialog = new AlertDialog.Builder(this);
                aboutDialog.setCancelable(true)
                        .setPositiveButton("OK", null)
                        .setView(view);

                final AlertDialog alertd = aboutDialog.create();
                        alertd.show();
        }

the output on second time click on about

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:4939)
        at android.view.ViewGroup.addView(ViewGroup.java:4770)
        at android.view.ViewGroup.addView(ViewGroup.java:4742)
        at androidx.appcompat.app.AlertController.setupCustomContent(AlertController.java:657)
        at androidx.appcompat.app.AlertController.setupView(AlertController.java:475)
        at androidx.appcompat.app.AlertController.installContent(AlertController.java:233)
        at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:403)
        at android.app.Dialog.show(Dialog.java:302)
        at www.pro_cs_is.com.MainActivity.onNavigationItemSelected(MainActivity.java:547)
        at com.google.android.material.navigation.NavigationView$1.onMenuItemSelected(NavigationView.java:174)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
        at com.google.android.material.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:368)
        at android.view.View.performClick(View.java:6311)
        at android.view.View$PerformClick.run(View.java:24833)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:173)
        at android.app.ActivityThread.main(ActivityThread.java:6653)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)

I tried most of solution in answered in this question 1, question 2, question 3 but unfortunately isn't working

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
  • You are re-adding the view to aboutDialog --> .setView(view); Re-create the view instance – Ravi Mar 19 '19 at 21:04
  • @Ravi yes I think this the reason of problem, so how to remove view from dialog if it already have view? – Dr Mido Mar 19 '19 at 21:06

1 Answers1

1

Remove the view from its parent view and re-add it to your dialog. Add the below line before adding view to dialog

To Remove

if(view.getParent()!=null)
        ((ViewGroup)view.getParent()).removeView(view); 
Ravi
  • 881
  • 1
  • 9
  • 23