Java 指定的子级已具有父级。必须对子对象&x27;调用removeView();她先是父母
我有一个类来创建对话框,并通过编码从中获取值。这对一个人来说很好。当我第二次尝试调用dialog时,它会传递以下错误消息 :java.lang.IllegalStateException:指定的子级已具有父级。必须首先对子级的父级调用removeView() 您能告诉我如何删除removeView()吗 下面是该类的代码Java 指定的子级已具有父级。必须对子对象&x27;调用removeView();她先是父母,java,android,Java,Android,我有一个类来创建对话框,并通过编码从中获取值。这对一个人来说很好。当我第二次尝试调用dialog时,它会传递以下错误消息 :java.lang.IllegalStateException:指定的子级已具有父级。必须首先对子级的父级调用removeView() 您能告诉我如何删除removeView()吗 下面是该类的代码 package com.util; import android.app.AlertDialog; import android.content.Context;
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();
}
});
这是我调用类实例的代码
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()
时,对话框被隐式初始化。因此,当您第二次调用dialog时,dialog构造函数也是如此。我在片段的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);
}
看看这个:
具体而言,LayoutFlator的充气方法中的布尔参数和返回值:
返回
膨胀层次结构的根视图。
如果提供了root并且attachToRoot为true,则这是root;
否则它就是膨胀的XML文件的根。
View dialogView = inflater.inflate(R.layout.brush_opts_dialog, rootView, false);
您想使膨胀视图的根成为已创建的视图,而不是“this”,这将是活动内部的整个片段。您是否在第二次尝试时再次调用对话框构造函数?我使用的所有代码都已复制到我的问题中。我想这可能就是原因。但我不知道如何避免这种情况?单击按钮时不要调用构造函数两次。使用对话框构造函数在
onCreate
中创建一次对话框,或者使用just code在onPrepareDailog
中创建一次对话框,然后在需要显示对话框时调用dialog.show()。在onCreate中,如上所述。我已经编辑了上面的代码。它是在mTxtShiftName.setOnClickListener处调用的。这不对吗?提前谢谢。