Java alertdialog-必须调用removeView
我有一个带有编辑文本区域的警报对话框。当我第二次调用它时,应用程序因错误而崩溃:Java alertdialog-必须调用removeView,java,android,android-edittext,android-alertdialog,Java,Android,Android Edittext,Android Alertdialog,我有一个带有编辑文本区域的警报对话框。当我第二次调用它时,应用程序因错误而崩溃: 02-28 23:25:08.958: E/AndroidRuntime(11533): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 这是我的密码: alert = new AlertDialog.
02-28 23:25:08.958: E/AndroidRuntime(11533): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
这是我的密码:
alert = new AlertDialog.Builder(this);
String txt_title = context.getResources().getString(R.string.txt_head_search_coord);
String txt_message = context.getResources().getString(R.string.txt_mess_search_coord);
alert.setTitle(txt_title);
alert.setMessage(txt_message);
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton(context.getResources().getString(R.string.Accept), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
// Do something with value!
dialog.dismiss();
}
});
alert.setNegativeButton(context.getResources().getString(R.string.Cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
//UTM Koordinate suchen
btn_search_coord.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
alert.show();
}
});
警报是全局定义的,因此我可以在onClickListener中调用它
我已经在取消对话框…
AlertDialog.Builder.show
从Builder
的内容创建一个Alert
的新实例,包括setView
中给出的视图
因此,您的输入将添加到警报中。要防止出现这种情况,请使用create
创建您的AlertDialog
的最终实例,并在此实例上调用show
:
final AlertDialog alertDialog = alert.create();
[...]
// in onClick
alertDialog.show();
从更广泛的角度来看,您应该使用showDialog(int-id)
和相关的方法onCreateDialog
和onPrepareDialog
。但是,如果您使用片段
,所有这些现在都被弃用,在这种情况下,您应该使用对话框片段
AlertDialog.Builder.show
从生成器
的内容创建一个警报
的新实例,包括setView
中给出的视图
因此,您的输入将添加到警报中。要防止出现这种情况,请使用create
创建您的AlertDialog
的最终实例,并在此实例上调用show
:
final AlertDialog alertDialog = alert.create();
[...]
// in onClick
alertDialog.show();
从更广泛的角度来看,您应该使用showDialog(int-id)
和相关的方法onCreateDialog
和onPrepareDialog
。但是,如果您使用片段
,则所有这些现在都不推荐使用,在这种情况下,您应该在我调用alert.show()的行中使用一个对话框片段
导致崩溃的行是什么;当你点击按钮两次时会发生吗?是的,在第一次调用时它工作,在第二次调用时它崩溃是哪一行导致崩溃在我调用alert.show()的那一行;当你点击按钮两次时会发生这种情况吗?是的,在第一次呼叫时它工作,在第二次呼叫时它崩溃