指定的小孩已经有一个父。你必须呼吁孩子的父母第一次removeView()(Android版) [英] The specified child already has a parent. You must call removeView() on the child's parent first (Android)

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

问题描述

在我的应用程序,我经常两个布局之间进行切换。该错误是发生在下面张贴的布局。

In my app, I have to switch between two layouts frequently. The error is happening in the layout posted below.

在我的布局叫做第一次,有没有存在的任何错误,一切都很好。当我然后调用不同的布局(空的),之后打电话给我布置了第二次,它给了我以下错误:

When my layout is called the first time, there isn't occuring any error and everything's fine. When I then call a different layout (a blank one) and afterwards call my layout a second time, it gives me the following error:

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

我的布局 - code是这样的:

My layout-code looks like this:

    tv = new TextView(getApplicationContext()); // are initialized somewhere else
    et = new EditText(getApplicationContext()); // in the code


private void ConsoleWindow(){
        runOnUiThread(new Runnable(){

     @Override
     public void run(){

        // MY LAYOUT:
        setContentView(R.layout.activity_console);
        // LINEAR LAYOUT
        LinearLayout layout=new LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);

        // TEXTVIEW
        layout.addView(tv); //  <==========  ERROR IN THIS LINE DURING 2ND RUN
        // EDITTEXT
        et.setHint("Enter Command");
        layout.addView(et);
        }
    }
}

我知道这个问题已经被问过,但它并没有在我的情况有所帮助。

I know this question has been asked before, but it didn't help in my case.

推荐答案

该错误消息说,你应该做的。

The error message says what You should do.

// TEXTVIEW
if(tv.getParent()!=null)
    ((ViewGroup)tv.getParent()).removeView(tv); // <- fix
layout.addView(tv); //  <==========  ERROR IN THIS LINE DURING 2ND RUN
// EDITTEXT

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

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