Android的 - 指定的孩子已经有一个父。你必须先调用removeView()对孩子的父 [英] Android - The specified child already has a parent. You must call removeView() on the child's parent first

查看:157
本文介绍了Android的 - 指定的孩子已经有一个父。你必须先调用removeView()对孩子的父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有几十个是询问有关此错误的问题,但没有提出的解决方案似乎适用于我的问题,至少我看到的。

I know there are dozens of questions that ask about this error, but none of the proposed solutions seem to apply to my problem, at least that I see.

下面是我的日志:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
    at android.view.ViewGroup.addView(ViewGroup.java:1871)
    at android.view.ViewGroup.addView(ViewGroup.java:1851)
    at com.android.internal.app.AlertController.setupView(AlertController.java:365)
    at com.android.internal.app.AlertController.installContent(AlertController.java:206)
    at android.app.AlertDialog.onCreate(AlertDialog.java:251)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
    at android.app.Dialog.show(Dialog.java:225)
    at com.company.MyApp.MyActivity$7.onItemClick(MyActivity.java:240)
    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
    at android.widget.ListView.performItemClick(ListView.java:3513)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)

下面是MyActivity.java的onCreate()。你可以看到一个对话框生成器显示,并给予一定的价值。无论 myDialogLayout myDialogBu​​ilder 是私有类成员

Here is MyActivity.java onCreate(). You can see that a Dialog Builder is shown and given some values. Both myDialogLayout and myDialogBuilder are private class members

@Override
public void onCreate( Bundle savedInstanceState )
{
    // ... a bunch of init code...

    // Create a dialog builder
    myDialogLayout = getLayoutInflater().inflate(R.layout.leaving, null);
    myDialogBuilder = new AlertDialog.Builder(this)
        .setTitle("My Title")
        .setView(myDialogLayout)
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            public void onClick( DialogInterface dialog, int which ) {}
        });

    // ... more code ...
}

之后,点击我的活动一定的按钮时,在 myDialogBu​​ilder 定制了一下,然后创建一个对话框,如图所示。 myDialog 是一个私有类变量/成员:

Later, when a certain button is clicked in my Activity, the myDialogBuilder is customized a bit and then a dialog is created and shown. myDialog is a private class variable/member:

// Builder customized a bit
myDialogBuilder 
    .setMessage("custom message here");

// Dialog created from Builder
myDialog = myDialogBuilder.create();

// Dialog shown
myDialog.show();  // <---- MyActivity.java Line: 240

所以到了我的问题,当我点击我的按钮,创建成功的对话框。但经过我preSS取消对话框,然后preSS一次按钮,我得到在日志中看到的错误。出于某种原因,该活动是不是要我再利用我的 myDialog 对话框。这是一个类的成员,所以它是从的onClick 处理程序访问。而 myDialog 是一个新的对话框每被点击一次按钮,因为它是从头开始由 myDialogBu​​ilder.create()每一次。

So onto my problem, when I click my button, the dialog is created successfully. But after I press Cancel on the dialog and then press the button again, I get the error seen in the log. For some reason, the activity isn't wanting me to reuse my myDialog dialog. It's a class member, so it is accessible from the onClick handler. And the myDialog is a new dialog every time the button is clicked, because it's created from scratch by the myDialogBuilder.create() every time.

任何人都知道是什么问题?我也试过在 myDialog.dismiss增加()时,取消按钮pssed $ P $但是这并没有发挥作用。

Anyone know what the problem is? I also tried adding in myDialog.dismiss() when the Cancel button is pressed but that didn't make a difference.

此外,您还可以看到我的 myDialogBu​​ilder 的自定义XML布局是用于对话的看法。根据错误信息,这听起来像它希望我用 removeView(),以便从对话框被用于除去视图。但 myDialogBu​​ilder.removeView()不是一个有效的方法。

Also, you can see in my myDialogBuilder that a custom XML layout is used for the Dialog's view. According to the error message, it sounded like it wants me to use removeView() in order to remove the view from being used in the Dialog. But myDialogBuilder.removeView() isn't a valid method.

推荐答案

myDialogLayout A类成员变量?如果是的话,它已经从第一次家长,你显示对话框,然后创建第二个对话框,也试图为 myDialogLayout 的母公司。尝试创建的 myDialogLayout 的新实例每次你打开对话框。

is myDialogLayout a class member variable? If so then it already has a parent from the first time you show the dialog then you create a second dialog which also tries to be the parent of myDialogLayout. Try creating a new instance of myDialogLayout everytime you open the dialog.

这篇关于Android的 - 指定的孩子已经有一个父。你必须先调用removeView()对孩子的父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆