В моей OnCreate(), у меня есть такой код:LinearLayout.addView - У указанного дочернего элемента уже есть родитель. Вы должны вызвать removeView() на родителе ребенка первого
@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() на родителя ребенка первого
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/guitarbody"
android:weightSum="1">
<LinearLayout
android:id="@+id/leftlayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="0.7"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/k0" android:text="chord" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.29"/>
</LinearLayout>
<LinearLayout
android:id="@+id/rightlayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.3"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
>
</LinearLayout>
</LinearLayout>
разместить XML-файл – arjun
макета обновленный вопрос с файлом XML. – JackPowell