0

I have this piece of code to display an alert dialog when an item in a listView is clicked; all worked ok until I added a checkbox preference that should enable another dialog to enter a text string (to be used later on).

AlertDialog.Builder helpBuilder = new AlertDialog.Builder(MyActivity.this);

helpBuilder.setIcon(android.R.drawable.ic_dialog_info);
adb.setTitle("title");
adb.setMessage("message");

        helpBuilder.setPositiveButton("positive", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                        boolean fileRenameEnabled = settings.getBoolean("text_view", false);
                        if (fileRenameEnabled == true) {
                            AlertDialog.Builder adb = new AlertDialog.Builder(MyActivity.this);
                            LayoutInflater adbInflater = LayoutInflater.from(MyActivity.this);
                            View inputFilename = adbInflater.inflate(R.layout.dialog_text_view, null);
                            tv = (TextView) inputFilename.findViewById(R.id.tv);
                            tv.setHint("hint");

                            adb.setView(tv);
                            adb.setTitle("New filename");

                            adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    //some code here modifies method1()
                                    method1();
                                }
                            });
                            adb.show();
                        } else {
                            method1();
                        }
                    }
                });

                helpBuilder.setNeutralButton("neutral", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        Checkbox1Enabled = settings.getBoolean("checkbox1", true);
                        if (Checkbox1Enabled == true) {
                            AlertDialog.Builder adb = new AlertDialog.Builder(MyActivity.this);
                            LayoutInflater adbInflater = LayoutInflater.from(MyActivity.this);
                            View info = adbInflater.inflate(R.layout.dialog_checkbox1, null);
                            cb = (CheckBox) info.findViewById(R.id.cb);
                            cb.setChecked(true);
                            adb.setView(info);
                            adb.setTitle("title");
                            adb.setMessage("message");

                            adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    if (cb.isChecked() == false) {
                                        SharedPreferences.Editor editor = settings.edit();
                                        editor.putBoolean("checkbox1", false);
                                        editor.commit();
                                        Checkbox1Enabled = settings.getBoolean("checkbox1", true);
                                        Log.d(DEBUG_TAG, "Checkbox1Enabled: " + Checkbox1Enabled);
                                    }
                                    method2(); 
                            }
                            });
                            adb.show();
                        } else {
                            method2();
                        }
                    }
                });
                AlertDialog helpDialog = helpBuilder.create();
                helpDialog.show();
            }
});

}

The fact is that I'm doing almost the same thing inside the onClickListener for the neutral button; the only difference is that I'm inflating a textView in the AlertDialog.Builder instead of a checkbox (that works...).

The exception is:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Any help is much appreciated. Thanks!

dentex
  • 3,223
  • 4
  • 28
  • 47

1 Answers1

0

You're adding the TextView as the content of the dialog instead of the inflated layout(and the TextView (most likely) already has a parent in the layout that you inflate). It should be:

View inputFilename = adbInflater.inflate(R.layout.dialog_text_view, null);
tv = (TextView) inputFilename.findViewById(R.id.tv);
tv.setHint("hint");
adb.setView(inputFilename);
user
  • 86,916
  • 18
  • 197
  • 190