Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java alertdialog-必须调用removeView_Java_Android_Android Edittext_Android Alertdialog - Fatal编程技术网

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()的那一行;当你点击按钮两次时会发生这种情况吗?是的,在第一次呼叫时它工作,在第二次呼叫时它崩溃