Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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();
            }   
        });
like image 265
SAN Avatar asked Feb 11 '12 23:02

SAN


2 Answers

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);
}
like image 91
user1678835 Avatar answered Oct 22 '22 08:10

user1678835


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.

like image 23
LazarusX Avatar answered Oct 22 '22 08:10

LazarusX