java – The specified child already has a parent. You must call removeView() on the child's parent first-ThrowExceptions

Exception or error:

I have a class to create the dialog and coding to get the values from it. it works fine for one. and when i try to call dialog for the second time it passes following error message.

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

Can you please tell me how to remove the removeView()?

here is the code for the class;

    package com.util;

import android.app.AlertDialog;  
import android.content.Context;  
import android.content.DialogInterface;  
import android.content.DialogInterface.OnClickListener;  
import android.widget.EditText;  

/** 
 * helper for Prompt-Dialog creation 
 */  
public abstract class PromptDialog extends AlertDialog.Builder implements OnClickListener {  
 private final EditText input;  

 /** 
  * @param context 
  * @param title resource id 
  * @param message resource id 
  */  
 public PromptDialog(Context context, int title, int message) {  
  super(context);  
  setTitle(title);
  //:TODO Display msg only if not empty
  //setMessage(message);  

  input = new EditText(context);  
  setView(input);  

  setPositiveButton("ok", this);  
  setNegativeButton("cancel", this);  
 }  

 /** 
  * will be called when "cancel" pressed. 
  * closes the dialog. 
  * can be overridden. 
  * @param dialog 
  */  
 public void onCancelClicked(DialogInterface dialog) {  
  dialog.dismiss();  
 }  

 @Override  
 public void onClick(DialogInterface dialog, int which) {  
  if (which == DialogInterface.BUTTON_POSITIVE) {  
   if (onOkClicked(input.getText().toString())) {  
    dialog.dismiss();  
   }  
  } else {  
   onCancelClicked(dialog);  
  }  
 }  

 /** 

      * called when "ok" pressed. 
      * @param input 
      * @return true, if the dialog should be closed. false, if not. 
      */  
     abstract public boolean onOkClicked(String input);  
    }  

and here is the code that i call the instance of the class;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



final PromptDialog dlgName = new PromptDialog(this, R.string.enterName, R.string.enter_comment) {  
             @Override  
             public boolean onOkClicked(String input) {  
              // do something 
              mName = input;
                  save();
                          //end do some thing
              return true; // true = close dialog  
             }  
        };      


    mTxtShiftName = (TextView) findViewById(R.id.shiftname);
            mTxtShiftName.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dlgName.show();
            }   
        });
How to solve:

I got this error from calling the wrong inflate method in my fragment’s onCreateView() call.

I fixed it by changing from this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_saves, container);
}

To this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_saves, container, false);
}

###

You should put the code calling the dialog constructor inside the onCreateDialog(int) callback method instead of onCreate(Bundle). In your code, the dialog is initialized implicitly when you call dlgName.show(). As a result, when you call dialog for the second time, so it is with the dialog constructor.

###

Check this out:

http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int,android.view.ViewGroup,boolean)

Specifically, the boolean argument and Return value in LayoutInflator’s inflate method:

Returns
The root View of the inflated hierarchy.
If root was supplied and attachToRoot is true, this is root;
otherwise it is the root of the inflated XML file.

View dialogView = inflater.inflate(R.layout.brush_opts_dialog, rootView, false);

You want to make the inflated view’s root to be the created view rather than “this”, which would be the entire fragment inside of an activity.

Leave a Reply

Your email address will not be published. Required fields are marked *