Android LinearLayout.addView-指定的子级已具有父级。必须对子对象&x27;调用removeView();她先是父母

Android LinearLayout.addView-指定的子级已具有父级。必须对子对象&x27;调用removeView();她先是父母,android,android-layout,Android,Android Layout,在我的onCreate()中,我有如下代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, Win

在我的onCreate()中,我有如下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main2);

    newMethod();

    getSupportActionBar().hide();


}
此newMethod()函数将调用此函数:

 private void setupScreen() {


    leftlayout = (LinearLayout) findViewById(R.id.leftlayout);
    if (leftlayout == null) {
        leftlayout = new LinearLayout(this);
        leftlayout.setOrientation(LinearLayout.VERTICAL);
        leftlayout.setGravity(Gravity.FILL_VERTICAL);
    }

    rightlayout = (LinearLayout) findViewById(R.id.rightlayout);
    if (rightlayout == null) {
        rightlayout = new LinearLayout(this);
        rightlayout.setOrientation(LinearLayout.VERTICAL);
        rightlayout.setGravity(Gravity.FILL_VERTICAL);
    }

    mainlayout = (LinearLayout) findViewById(R.id.mainlayout);
    if (mainlayout == null) {
        mainlayout = new LinearLayout(this);
        mainlayout.setOrientation(LinearLayout.HORIZONTAL);
        mainlayout.setGravity(Gravity.FILL_VERTICAL);
    }

    loadButtons(defaultChords);

    mainlayout.addView(leftlayout);

    mainlayout.addView(rightlayout);

    //this.setContentView(R.layout.activity_main2);
    this.setContentView(mainlayout);
}
错误基于以下行:
mainlayout.addView(leftlayout)

指定的子级已具有父级。必须首先对子级的父级调用removeView()


错误意味着您正在将一个
视图
作为子视图添加到另一个父视图
布局
。 因此,在上面的代码中,当执行
if
条件时,这意味着无论何时在运行时创建新的
布局
,它都可以正常工作,因为它没有父级。 但是,当使用
findViewById()
方法创建布局时,创建的布局将已经具有父级,因此将抛出错误

甚至布局xml文件的根元素也将是系统生成的父元素的子元素,它将附加到该父元素

因此,解决方案是始终在运行时创建
视图
,并将其添加到父布局中

从xml文件中删除
leftlayout
rightlayout
布局,然后根据需要创建并添加这些布局

或者试试这个

mainlayout = (LinearLayout) findViewById(R.id.mainlayout);
if (mainlayout == null) {
    mainlayout = new LinearLayout(this);
    mainlayout.setOrientation(LinearLayout.HORIZONTAL);
    mainlayout.setGravity(Gravity.FILL_VERTICAL);
}

leftlayout = (LinearLayout) findViewById(R.id.leftlayout);
if (leftlayout == null) {
    leftlayout = new LinearLayout(this);
    leftlayout.setOrientation(LinearLayout.VERTICAL);
    leftlayout.setGravity(Gravity.FILL_VERTICAL);
    mainlayout.addView(leftlayout)
}

rightlayout = (LinearLayout) findViewById(R.id.rightlayout);
if (rightlayout == null) {
    rightlayout = new LinearLayout(this);
    rightlayout.setOrientation(LinearLayout.VERTICAL);
    rightlayout.setGravity(Gravity.FILL_VERTICAL);
    mainlayout.addView(rightlayout)
}
this.setContentView(mainlayout);

在活动的
onCreate()
中尝试以下代码:

setContentView(R.layout.activity_main2);
LinearLayout Main_layout=(LinearLayout)findViewById(R.id.mainlayout);
//remove parent view first
((ViewGroup)Main_layout.getParent()).removeView(Main_layout);
newMethod();

发布布局xml文件用xml文件更新了问题。感谢您的回复,如果我想以这种方式保存代码,我应该如何修改代码?正如我提到的,不要从布局创建视图。删除那些使用
findViewById()
方法创建布局的行这可能有效,但不推荐,因为这就像是在xml文件中添加视图,然后在onCreate中删除它,然后再次创建相同的视图并设置为layoutHi,我已经尝试了代码,没有显示错误(我不知道),但是!android studio在设备上执行应用程序时,应用程序不会出现在设备上screen@JackPowell我不知道;无法理解应用程序从未出现在设备屏幕上
??@rafsanahmad007,这意味着在android studio将应用程序执行到设备中后,应用程序从未出现在设备屏幕上
mainlayout = (LinearLayout) findViewById(R.id.mainlayout);
if (mainlayout == null) {
    mainlayout = new LinearLayout(this);
    mainlayout.setOrientation(LinearLayout.HORIZONTAL);
    mainlayout.setGravity(Gravity.FILL_VERTICAL);
}

leftlayout = (LinearLayout) findViewById(R.id.leftlayout);
if (leftlayout == null) {
    leftlayout = new LinearLayout(this);
    leftlayout.setOrientation(LinearLayout.VERTICAL);
    leftlayout.setGravity(Gravity.FILL_VERTICAL);
    mainlayout.addView(leftlayout)
}

rightlayout = (LinearLayout) findViewById(R.id.rightlayout);
if (rightlayout == null) {
    rightlayout = new LinearLayout(this);
    rightlayout.setOrientation(LinearLayout.VERTICAL);
    rightlayout.setGravity(Gravity.FILL_VERTICAL);
    mainlayout.addView(rightlayout)
}
this.setContentView(mainlayout);
setContentView(R.layout.activity_main2);
LinearLayout Main_layout=(LinearLayout)findViewById(R.id.mainlayout);
//remove parent view first
((ViewGroup)Main_layout.getParent()).removeView(Main_layout);
newMethod();