Something I got confused, the dialog box opens when the first launch occurs, and when you call it again, it crashes:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

I understand what this error is, but I also give the basic context

 public void createAlertInfo() { imquastion.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(PreviewActivity.this); builder.setTitle(R.string.app_name).setMessage(R.string.quastion) .setIcon(R.drawable.icon_512).setCancelable(false).setView(mImagealert) .setNegativeButton(R.string.alertcancelbt, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.show(); } }); } 

when restarting the dialog, the window falls on this line:

  builder.show(); 

Inherited from extends Activity

    1 answer 1

    According to the error, you are trying to add markup to the dialog that is already used in another dialog. He did not die after his close and still holds a link to the markup.

    You do not need to reuse the same markup each time, but download it again for each dialogue display.

     public void createAlertInfo() { imquastion.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { View mImagealert = ...; //каждый раз заново загружаем разметку через LayoutInflater AlertDialog.Builder builder = new AlertDialog.Builder(PreviewActivity.this); builder.setTitle(R.string.app_name).setMessage(R.string.quastion) .setIcon(R.drawable.icon_512).setCancelable(false).setView(mImagealert) .setNegativeButton(R.string.alertcancelbt, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.show(); } }); } 
    • @upward, i.e. if, as in the code, I wrote to replace the field of the class with a local method variable, is it still a departure? .. Can you have InstantRun enabled? In any case, the markup for the dialogue does not make sense outside the method of displaying it. especially if it is a class field - YuriySPb
    • one
      @upward, you confused me. Do you work or not? And if so, what did you do? If not - the same question. - Yuriy SPb
    • @Yuriy SPb, everything works, thanks. - upward