Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.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 Android-AlertDialog.Builder在第二次启动时崩溃_Java_Android - Fatal编程技术网

Java Android-AlertDialog.Builder在第二次启动时崩溃

Java Android-AlertDialog.Builder在第二次启动时崩溃,java,android,Java,Android,我正在打开一个AlertDialog,向用户提供一个文本输入,以便命名一个新项目。这在第一次打开时工作正常。但当我第二次单击启动对话框的按钮时,应用程序崩溃了。我得到这个错误: 12-02 16:01:04.205: E/AndroidRuntime(515): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's p

我正在打开一个AlertDialog,向用户提供一个文本输入,以便命名一个新项目。这在第一次打开时工作正常。但当我第二次单击启动对话框的按钮时,应用程序崩溃了。我得到这个错误:

12-02 16:01:04.205: E/AndroidRuntime(515): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我不确定这是什么意思,也不确定我将把它称为
removeView()

这是我的密码:

public class ShoppingList extends Activity implements OnClickListener{

        private AlertDialog.Builder m_alert;
        private Context m_context;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.shopping_list);
            m_context = getApplicationContext();
            LayoutInflater inflater = (LayoutInflater) m_context.getSystemService(LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.add_shopping_list, (ViewGroup) findViewById(R.id.layout_root));
            m_alert = new AlertDialog.Builder(ShoppingList.this);
            //final EditText input = new EditText(this);
            //m_alert.setView(input);
            m_alert.setView(layout);
            final EditText input = (EditText)layout.findViewById(R.id.new_sl_name);
            m_alert.setPositiveButton(R.string.add_button, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String value = input.getText().toString().trim();
                    Toast.makeText(m_context, value,
                            Toast.LENGTH_SHORT).show();
                    m_alert.create();
                }
            });

            m_alert.setNegativeButton(R.string.cancel_button,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                        m_alert.create();
                    }
                }
            );

            // Add Button. . .
            Button addButton = (Button)findViewById(R.id.add_sl_button);
            addButton.setOnClickListener(this);
        }

        public void onClick(View v) {
            if(!this.isFinishing())
                m_alert.show();
        }
    }

这很可能是因为您正在使用
m_alert.create()在错误的位置

在对话框上查看本教程:。

m_alert.create()在单击正按钮和负按钮后调用。这样做的目的是再次创建对话框


改用m_alert.disease(),这样您的对话框将被取消,您可以稍后再次使用它

这可能会对某些人有所帮助。我的大部分应用程序都是在笔记本电脑上开发的。我刚把我的办公室重新组装好,我的电脑启动了。当我试图从我的云存储在我的pc上加载我的项目时,似乎发生了一些构建错误。对于AlertDialog.Builder崩溃,我注意到,由于某种原因,我在新安装的Android Studio上的电脑将我的“导入”设置为Android.support.v7.app.AlertDialog,而不是Android.app.AlertDialog。我删除了导入,重新选择了android.app.AlertDialog,一切正常。我不知道这是不是一个bug,也不知道Android Studio是如何默认安装文件的。对于类似的错误,删除所有导入文件并重新选择您知道适用的文件可能是值得的。祝你好运

你能发布完整的stacktrace吗?它应该指出失败的行号。谢谢!这就是问题所在。我觉得我可以重复使用这个对话。