I know there are dozens of questions that ask about this error, but none of the proposed solutions seem to apply to my problem, at least that I see.
Here's my log:
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:1976)
at android.view.ViewGroup.addView(ViewGroup.java:1871)
at android.view.ViewGroup.addView(ViewGroup.java:1851)
at com.android.internal.app.AlertController.setupView(AlertController.java:365)
at com.android.internal.app.AlertController.installContent(AlertController.java:206)
at android.app.AlertDialog.onCreate(AlertDialog.java:251)
at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
at android.app.Dialog.show(Dialog.java:225)
at com.company.MyApp.MyActivity$7.onItemClick(MyActivity.java:240)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3513)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Here is MyActivity.java onCreate(). You can see that a Dialog Builder is shown and given some values. Both myDialogLayout
and myDialogBuilder
are private class members
@Override
public void onCreate( Bundle savedInstanceState )
{
// ... a bunch of init code...
// Create a dialog builder
myDialogLayout = getLayoutInflater().inflate(R.layout.leaving, null);
myDialogBuilder = new AlertDialog.Builder(this)
.setTitle("My Title")
.setView(myDialogLayout)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick( DialogInterface dialog, int which ) {}
});
// ... more code ...
}
Later, when a certain button is clicked in my Activity, the myDialogBuilder
is customized a bit and then a dialog is created and shown. myDialog
is a private class variable/member:
// Builder customized a bit
myDialogBuilder
.setMessage("custom message here");
// Dialog created from Builder
myDialog = myDialogBuilder.create();
// Dialog shown
myDialog.show(); // <---- MyActivity.java Line: 240
So onto my problem, when I click my button, the dialog is created successfully. But after I press Cancel on the dialog and then press the button again, I get the error seen in the log. For some reason, the activity isn't wanting me to reuse my myDialog
dialog. It's a class member, so it is accessible from the onClick
handler. And the myDialog
is a new dialog every time the button is clicked, because it's created from scratch by the myDialogBuilder.create()
every time.
Anyone know what the problem is? I also tried adding in myDialog.dismiss()
when the Cancel button is pressed but that didn't make a difference.
Also, you can see in my myDialogBuilder
that a custom XML layout is used for the Dialog's view. According to the error message, it sounded like it wants me to use removeView()
in order to remove the view from being used in the Dialog. But myDialogBuilder.removeView()
isn't a valid method.
is myDialogLayout
a class member variable? If so then it already has a parent from the first time you show the dialog then you create a second dialog which also tries to be the parent of myDialogLayout
. Try creating a new instance of myDialogLayout
everytime you open the dialog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With