指定的孩子已经有父母。您必须首先调用子视图的父级的removeView()
我有一个类来创建对话框和编码以从中获取值。它适用于一个。当我尝试第二次调用对话框时,它传递以下错误消息。指定的孩子已经有父母。您必须首先调用子视图的父级的removeView()
: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();
}
});
你应该把代码调用onCreateDialog(int)
回调方法,而不是onCreate(Bundle)
内部对话框构造。在您的代码中,当您拨打dlgName.show()
时,该对话框将被隐式初始化。因此,当您第二次调用对话框时,对话框构造函数就是如此。
我从我的片段的onCreateView()调用中调用错误的膨胀方法得到此错误。
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);
}
花花公子作为最后一个参数拯救了我的生命!谢谢! – 2014-07-25 00:16:11
给那个男人一枚奖章!谢啦 – Karoly 2016-03-20 22:48:05
检查了这一点:
具体而言,布尔参数和返回值
我通过从该改变固定它在LayoutInflator的膨胀方法中:
返回 充气层次结构的根视图。 如果提供了root并且attachToRoot为true,则这是根; 否则它是膨胀的XML文件的根。
View dialogView = inflater.inflate(R.layout.brush_opts_dialog, rootView, false);
你想使充气视图的根是创建的视图,而不是“本”,这将是一个活动的整个内部片段。
你在第二次尝试时再次调用对话框构造函数吗? – 2012-02-12 00:01:56
我使用的所有编码都在我的问题中复制。我认为这可能是原因。但我不知道如何避免这种情况? – SAN 2012-02-12 00:22:21
单击按钮时不要调用构造函数两次。在onCreate中使用Dialog构造函数或onPrepareDailog使用代码创建对话框,然后在想要显示时调用dialog.show()。 – 2012-02-12 00:25:22