Android嵌套对话框 - 指定的子级已有父级

时间:2014-02-17 19:52:31

标签: android alertdialog

我试图在第二个对话框上显示一个带有后退按钮的嵌套对话框,单击该对话框,用户将显示第一个对话框。我的代码如下:

public class FileOpenMenuDelegate {
    AlertDialog.Builder fileOpenBuilderFolder = null;
    AlertDialog fileOpenBuilderFolderDialog = null;
    AlertDialog.Builder fileOpenBuilder = null;
    AlertDialog fileOpenBuilderDialog = null;

    public void setSelectedItem(int position) {
        selectedItem = position;
    }

    public FileOpenMenuDelegate(Context context, View view) {
        this.context = context;
        this.view = view;
        fileOpenBuilderFolder = new AlertDialog.Builder(context);
        fileOpenBuilder = new AlertDialog.Builder(context);
    }

    public void fileOpenMenu() {

        final CharSequence[] foldernames = getAllFolders();


        if (foldernames == null) {
            //Handle
        } else {

            LayoutInflater inflater = LayoutInflater.from(context);
            View convertView = (View) inflater.inflate(R.layout.dialog_list_layout, null);

            ListView lv = (ListView) convertView.findViewById(R.id.listExample);

            selectedAdapter = new CustomAdapter(context, 0, Arrays.asList(foldernames));
            lv.setAdapter(selectedAdapter);

            fileOpenBuilderFolder.setTitle(FAC.OPEN_FOLDERS);
            fileOpenBuilderFolder.setView(convertView);

            selectedAdapter.setSelectedPosition(2);

            lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView arg0, View view,
                                        int position, long id) {
                    fileOpenBuilderFolderDialog.dismiss();
                    selectedAdapter.setSelectedPosition(position - 1);

                    String selectedFolder = foldernames[position].toString();

                    //Sub window

                    final CharSequence[] allFiles = getAllFiles();

                    LayoutInflater inflater = LayoutInflater.from(context);
                    View convertView = (View) inflater.inflate(R.layout.dialog_list_layout, null);
                    ListView subListView = (ListView) convertView.findViewById(R.id.listExample);
                    fileOpenBuilder.setTitle("Test");

                    subMenuAdapter = new CustomAdapter(context, 0, Arrays.asList(allFiles));
                    subListView.setAdapter(subMenuAdapter);

                    fileOpenBuilder.setView(convertView);

                    subListView.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView arg0, View view,
                                                int position, long id) {
                            fileOpenBuilderDialog.dismiss();
                            if (position == 0) {
                                fileOpenBuilderFolderDialog.dismiss();
                                fileOpenBuilderFolderDialog = fileOpenBuilderFolder.create();
                                fileOpenBuilderFolderDialog.show();************Error Line***************
                            }
                        }
                    });

                    fileOpenBuilderDialog = fileOpenBuilder.create();
                    fileOpenBuilderDialog.show();
                }
            });

        }

        fileOpenBuilderFolderDialog = fileOpenBuilderFolder.create();
        fileOpenBuilderFolderDialog.show();
    }

}

一切正常,除非我点击第二个警报中的第一个项目,我已经编写了再次显示第一个对话框的代码 - 这给了我错误“指定的孩子已经有父错误”。我究竟做错了什么 ?请帮忙。

异常日志::

            02-17 12:38:59.250: E/AndroidRuntime(27702): FATAL EXCEPTION: main
    02-17 12:38:59.250: E/AndroidRuntime(27702): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3672)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.view.ViewGroup.addView(ViewGroup.java:3543)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.view.ViewGroup.addView(ViewGroup.java:3519)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at com.android.internal.app.AlertController.setupView(AlertController.java:402)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at com.android.internal.app.AlertController.installContent(AlertController.java:242)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.app.AlertDialog.onCreate(AlertDialog.java:336)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.app.Dialog.dispatchOnCreate(Dialog.java:375)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.app.Dialog.show(Dialog.java:261)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at edu.delegates.FileOpenMenuDelegate$2$1.onItemClick(FileOpenMenuDelegate.java:196)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.widget.AbsListView.performItemClick(AbsListView.java:1287)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3078)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.widget.AbsListView$1.run(AbsListView.java:4161)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.os.Handler.handleCallback(Handler.java:615)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.os.Handler.dispatchMessage(Handler.java:92)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.os.Looper.loop(Looper.java:137)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at android.app.ActivityThread.main(ActivityThread.java:4921)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at java.lang.reflect.Method.invoke(Method.java:511)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
    02-17 12:38:59.250: E/AndroidRuntime(27702):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

由于列表视图是第一个警报对话框的子项,因此出现错误。我用

修复了它
((ViewGroup) mainListView.getParent()).removeView(mainListView);

在创建新对话框之前。这是解决问题的方法。

答案 1 :(得分:1)

您正在尝试添加已有父级的View。这是什么?

at edu.asu.fact.delegates
   .FileOpenMenuDelegate$2$1
   .onItemClick(FileOpenMenuDelegate.java:196)

修改

无需将AlertDialogAlertDialog.Builder变量保留为类成员。尝试在每次需要时在本地创建它们。