简体   繁体   中英

Android - AlertDialog.Builder crashes when launching it a second time

I am opening an AlertDialog to present the user a text input in order to name a new item. This works fine the first time it opens. But the second time I click the button that launches the dialog, the app crashes. I get this error:

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.

I'm not sure what this means or where I would call this removeView() .

Here is my code:

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();
        }
    }

This is most likely because you are using m_alert.create(); in a wrong place.

Check this tutorial on dialogs: Dialogs .

m_alert.create() is called after clicking on the positive and negative buttons. What this does, is create the dialog again.

Use m_alert.dismiss() instead, so your dialog is dismissed, and you can use it again later

您可以在活动中覆盖onCreateDialog()以创建对话框,这将帮助您在此处查看有关对话框的详细信息

This might help someone. I build most of my apps on my laptop. I just got my office put back together and got my pc fired up. When I tried to load my projects from my cloud storage on my pc it seemed like there were a few build errors that happened. For AlertDialog.Builder Crash I notice that my pc on a fresh Android Studio install set my "import" to android.support.v7.app.AlertDialog instead of android.app.AlertDialog for some reason. I deleted the import and re selected the android.app.AlertDialog and everything worked like normal. I don't know if this is a bug or just how Android Studio installs files as a default. For similar errors might be worth while deleting all the import files and re selecting the ones you know apply. Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM