【问题标题】:dialog - The specified child already has a parent. You must call removeView() on the child's parent first对话框 - 指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()
【发布时间】:2016-09-24 04:25:04
【问题描述】:

在检查要求用户打开互联网服务并尝试单击按钮后,我的应用程序崩溃并显示错误消息

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

在这条线上它崩溃了,我试过这样做但没有完全解决

if(alert.getContext() != null){
            alert.show();
        }

这是完整的代码

else if (id == R.id.xyz) {

            //startActivity(borrowIntent);
            AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            alert.setTitle("xyz");
            input.setFilters(new InputFilter[] {
                    // Maximum 2 characters.
                    new InputFilter.LengthFilter(6),
                    // Digits only.
                    DigitsKeyListener.getInstance(), 
                });
            // Digits only & use numeric soft-keyboard.
            input.setKeyListener(DigitsKeyListener.getInstance());
            input.setHint("xyz");
            alert.setView(input);
            alert.setPositiveButton("Borrow", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                if(input.getText().length() == 0)
                {
                    input.setError("xyz is required !");
                }
                else
                {

                   if(isNetworkAvailable())
                      {
                         xyz( input.getText().toString());

                      }else{

                            //setContentView(R.layout.main);

                            AlertDialog.Builder builder = new AlertDialog.Builder(
                                    MainActivity.this);
                              builder.setCancelable(false);
                              builder.setTitle("xyz");
                              builder.setMessage("Please enable wifi services");
                              builder.setInverseBackgroundForced(true);
                              builder.setPositiveButton("Ok",
                                      new DialogInterface.OnClickListener() {
                                          @Override
                                          public void onClick(DialogInterface dialog,
                                                  int which) {

                                              startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
                                              dialog.dismiss();


                                          }
                                      });
                              AlertDialog alerts = builder.create();
                              alerts.show();
                           }//end of block

                        }
              }
            });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                // Canceled.
              }
            });
            if(alert.getContext() != null){
            alert.show(); //crashes at this line
            }
        }

请问我错过了什么?

【问题讨论】:

  • 您能否粘贴您正在使用该对话框的活动或片段代码

标签: java android


【解决方案1】:

问题出在这一行: alert.setView(input); 您添加了 input View 已经有 parent。 创建新的input 实例。

【讨论】:

【解决方案2】:

下面一行

 final AlertDialog alertd = alert.create();

之后

 AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

【讨论】:

  • 输入实际上是一个文本字段。所以删除用户将有字段输入数据
  • 对话框在片段或活动中
  • 输入是对Edittext的引用
  • 我必须修改我的代码。因为我找不到代码
  • 那么我现在将在哪里使用这个句柄警报
【解决方案3】:

根据此post,添加此检查以从其父级中删除输入并读取:

if(input.getParent()!=null)
        ((ViewGroup)input.getParent()).removeView(input); // <- fix
    alert.addView(input);

【讨论】:

    【解决方案4】:

    以下情况也可能发生(发生在我身上):

    有时,当您使用列表视图时,您会使用属于某个布局的适配器对其进行初始化。现在假设这个布局文件的根视图是一个&lt;LinearLayout&gt;,id 为“root_view”。

    如果您现在在 Activity 中注册上下文菜单并创建一个 AlerdDialog.Builder,它会在选择某个菜单元素后出现并使用布局文件对其进行初始化,该布局文件还有一个名为“root_view”的根元素,其中属于您的 AlertDialog 的所有元素都是它的子元素,那么这些元素“将找不到”。您将无法使用findViewById 访问这些元素,而只能从列表视图中访问这些元素,并且您在调用builder.show() 的行会收到相同的错误消息(或者这里的情况为@ 987654326@).

    因此,通常最好为您的项目在布局文件中唯一地命名元素的 ID。

    【讨论】:

      【解决方案5】:

      我忘了在AlertDialog.Builder 上拨打create()。当您调用 show() 而不调用 create() 方法时,将创建 AlertDialog 实例。这第一次有效,但随后的点击得到了IllegalStateException。当我在onClickListener 中调用show() 时,每次单击按钮时它都会创建一个新的AlertDialog 实例。

      【讨论】:

        猜你喜欢
        • 2013-10-18
        • 2018-12-03
        • 2015-09-06
        • 2020-02-16
        • 2014-06-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多