Dialog - The Specified Child Already Has A Parent. You Must Call RemoveView() On The Child's Parent First
Solution 1:
The problem is on this line:
alert.setView(input);
You added input
View
that have already parent
.
Create new input
instance.
Solution 2:
according to this post, add this check to remove input from it's parent and readd it:
if(input.getParent()!=null)
((ViewGroup)input.getParent()).removeView(input); // <- fix
alert.addView(input);
Solution 3:
Put following line
final AlertDialog alertd = alert.create();
After
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
Solution 4:
Following situation can also happen (happened to me):
Sometimes when you use a listview you initialize it with an adapter, which belongs to a certain layout. Now lets say the root view of this layout file is a <LinearLayout>
with the id "root_view".
If you register now for a context menu in your activity and create an AlerdDialog.Builder
which appears after choosing a certain menu element and initialize it with a layout file, which also has a root element with an id called "root_view" where all elements which belong to your AlertDialog
are children of it, then those elements "will not be found". You will not be able to access those elements with findViewById
, instead you can only access the elements from the of your list view and you get the same error message at the line where you call builder.show()
(or in the case here alert.show()
).
So generally it is a good idea to name the ids of your elements in the layout files uniquely, for your project.
Solution 5:
I forgot to call create()
on the AlertDialog.Builder
. When you call show()
without calling the create()
method, the AlertDialog
instance gets created. This worked the first time but then subsequent clicks got the IllegalStateException
. As I was calling show()
inside of my onClickListener
, it was going to create a new AlertDialog
instance every time button was clicked.
Post a Comment for "Dialog - The Specified Child Already Has A Parent. You Must Call RemoveView() On The Child's Parent First"