指定的子项已有父项。您必须首先在孩子的父母上调用removeView()

时间:2012-02-11 23:53:57

标签: java android

我有一个类来创建对话框和编码以从中获取值。它适用于一个人。当我第二次尝试调用对话框时,它会传递以下错误消息。

  

:java.lang.IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母身上调用removeView()。

您能否告诉我如何删除removeView()?

这是该类的代码;

    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);  
    }  

这是我称之为类实例的代码;

@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();
            }   
        });

3 个答案:

答案 0 :(得分:66)

我在片段的onCreateView()调用中调用错误的inflate方法时遇到此错误。

我修改了它:

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

对此:

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

答案 1 :(得分:1)

您应该将调用对话框构造函数的代码放在onCreateDialog(int)回调方法中,而不是onCreate(Bundle)。在您的代码中,当您调用dlgName.show()时,会隐式初始化对话框。因此,当您第二次调用对话框时,它就是对话框构造函数。

答案 2 :(得分:0)

检查出来:

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

具体来说,LayoutInflator的inflate方法中的boolean参数和Return值:

<强>返回     膨胀层次结构的根视图。     如果提供了root并且attachToRoot为true,则为root;     否则它是膨胀的XML文件的根。

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

您希望将膨胀视图的根目录设置为创建的视图而不是“this”,这将是活动内的整个片段。