How to remove view of child parent? android

So, I have a button that displays a warning dialog when clicked. I am creating a view for the warning dialog in the onCreate method of my activity. The code for this is here:

LayoutInflater factory = LayoutInflater.from(this); view = factory.inflate(R.layout.grade_result, null); 

When I click the button for the first time, the dialog box displays the way I want it, but when I click it the second time, it throws this exception

11-28 00: 35: 58.066: E / AndroidRuntime (30348): caused by: java.lang.IllegalStateException: the specified child already has a parent. You must first call removeView () on the parent parent.

My code for the method that displays AlertDialog when the button is pressed is here:

 public void details(View v){ final AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setView(view); alert.setMessage("Details About Your Grades") .setCancelable(false) .setPositiveButton("Continue", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); alert.show(); 

Any help would be appreciated! Thanks!

+6
source share
7 answers

Extending the view that should be set in Builder from AlertDialog worked for me:

 Button mButton = (Button) findViewById(R.id.my_button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // inflating view out of the onClick() method does not work LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); final ViewGroup viewGroup= (ViewGroup) mInflater.inflate(R.layout.my_view, null); Dialog alertDialog = new AlertDialog.Builder(getActivity()) .setTitle(R.string.my_title) .setView(viewGroup) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).create(); alertDialog.show(); } } 
+5
source

Use alert.create (); then alertd.dismiss (); to destroy the dialogue

  final AlertDialog.Builder alert = new AlertDialog.Builder(this); final AlertDialog alertd = alert.create(); alert.setTitle("Title"); alert.setMessage("Messaage"); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. dialog.cancel(); alertd.dismiss(); } }); 
0
source

Use this code

 final AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setView(view); alert.setMessage("Details About Your Grades") .setCancelable(false) .setPositiveButton("Continue", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); AlertDialog alertdialog = alert.create(); alertdialog .show() 
0
source

Use this:

 final AlertDialog.Builder alert = new AlertDialog.Builder(ProductsListAfterFilter.this); alert.setTitle("   ?"); alert.setMessage(" : "); // Set an EditText view to get user input final EditText input = new EditText(ProductsListAfterFilter.this); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { int value = 0 ; try { value = Integer.valueOf(input.getText().toString()); } catch (Exception err){ Toast.makeText(getApplicationContext(), " ", 100).show(); } //insertValue(value,st1,st2,st3,st4); Toast.makeText(getApplicationContext(), "value = "+value, 100).show(); dialog.dismiss(); dialog.cancel(); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. dialog.cancel(); dialog.dismiss(); } }); final AlertDialog alertd = alert.create(); alertd.show(); 
0
source

I had a similar problem with the warning dialog. The first time I fanned the view, and the second time I wanted to display a message. If you pop up the warning dialog once, you inflate the view and the dialog is cached. If you display it a second time, you will get an IllegalStateException: "first call removeView () on the parent parent"

I solved this problem by deleting the view that I puffed on the first click, and displayed a message on the second click. - Change the view or message each time before inflating or creating a message.

 alertDialogBuilder.setView(null).setMessage(null); alertDialogBuilder.setView(yourView).setMessage(yourMessage); 

I changed your code and it works great with me.

 LayoutInflater factory = LayoutInflater.from(this); mView = factory.inflate(R.layout.grade_result, null); public void details(View v){ final AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setView(null).setMessage(null); alert.setView(mView); alert.setMessage("Details About Your Grades") .setCancelable(false) .setPositiveButton("Continue", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); alert.show(); } 
0
source

It says that your β€œappearance” was associated with the parent. This happened when you called .setView (view). If I'm right, the alertDialog class provides the parent element for your view. This is the basics for Android. There can be many child views in a ViewGroup. I had a similar problem. So you can try this insinde onClick:

 ViewGroup vGroup = view.getParent(); vGroup.removeView(view); // And right after that - close dialog dialog.dismiss(); 

It helped me, hope it helps you!

0
source

You can set the View to null before setting the view in the dialog box.
just:

 alert.setView(null); alert.setView(view); 

It will always set the null view until it is reused.

-1
source

All Articles