XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.utente.test_dimensions.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="160dp"
android:layout_height="wrap_content"
android:digits="123456789"
android:hint="ID promemoria"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rimuovi prom." />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
爪哇:
package com.example.utente.test_dimensions;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout main = (LinearLayout)findViewById(R.id.main);
LinearLayout ll_eti = new LinearLayout(this);
ll_eti.setOrientation(LinearLayout.VERTICAL);
ll_eti.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
main.addView(ll_eti);
TextView eti_txtv = new TextView(this);
eti_txtv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
eti_txtv.setText("Etichetta:");
ll_eti.addView(eti_txtv);
TextView val_eti_txtv = new TextView(this);
val_eti_txtv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
HorizontalScrollView eti_hsv = new HorizontalScrollView(this);
eti_hsv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
eti_hsv.addView(val_eti_txtv);
ll_eti.addView(eti_hsv);
LinearLayout ll_info = new LinearLayout(this);
ll_info.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 0, 0);
ll_info.setLayoutParams(params);
main.addView(ll_info);
LinearLayout ll_date = new LinearLayout(this);
ll_date.setOrientation(LinearLayout.HORIZONTAL);
ll_date.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
ll_info.addView(ll_date);
TextView date = new TextView(this);
date.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
date.setText("01/01/2000");
ll_date.addView(date);
TextView perm_not_y_or_n = new TextView(this);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params1.setMargins(10, 0, 0, 0);
perm_not_y_or_n.setText("without permanent notificationW");
ll_date.addView(perm_not_y_or_n);
LinearLayout ll_details = new LinearLayout(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.setMargins(0, 3, 0, 0);
ll_details.setOrientation(LinearLayout.HORIZONTAL);
ll_details.setLayoutParams(params2);
ll_info.addView(ll_details);
TextView perm_val = new TextView(this);
perm_val.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
perm_val.setText("Repetitive notification:");
ll_details.addView(ll_info);
TextView var_val = new TextView(this);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params3.setMargins(5, 0 , 0 , 0);
var_val.setLayoutParams(params3);
var_val.setText("every 100 minutes");
ll_details.addView(var_val);
LinearLayout ll_id = new LinearLayout(this);
ll_id.setOrientation(LinearLayout.VERTICAL);
ll_id.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
main.addView(ll_id);
TextView ID_txtv = new TextView(this);
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params4.setMargins(0, 5, 0, 0);
ID_txtv.setLayoutParams(params4);
ID_txtv.setText("ID:");
ll_id.addView(ID_txtv);
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
horizontalScrollView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
ll_id.addView(horizontalScrollView);
TextView id_txtv = new TextView(this);
id_txtv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
id_txtv.setText("p");
horizontalScrollView.addView(id_txtv);
}
}
这里是logcat:
07-10 14:50:13.023 27776-27776/com.example.utente.test_dimensions E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.utente.test_dimensions, PID: 27776
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.utente.test_dimensions/com.example.utente.test_dimensions.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2352)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2414)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1318)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5282)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:375)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: 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:3937)
at android.view.ViewGroup.addView(ViewGroup.java:3787)
at android.view.ViewGroup.addView(ViewGroup.java:3728)
at android.view.ViewGroup.addView(ViewGroup.java:3701)
at com.example.utente.test_dimensions.MainActivity.onCreate(MainActivity.java:67)
at android.app.Activity.performCreate(Activity.java:6865)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2414)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1318)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5282)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:375)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
我正在测试为xml文件添加视图(对于我的应用); 我无法理解为什么这不起作用,你有什么建议吗? 我看到每个人都使用addView()将视图添加到布局中,但为什么这对我不起作用?
答案 0 :(得分:2)
您不能将同一视图添加到两个不同的父级,也不能将视图添加到父级两次。
您似乎正在尝试将ll_info
视图添加到main
视图和ll_details
。
答案 1 :(得分:1)
您的问题是您正在尝试将视图添加到另一个视图,但该视图已添加到其他视图中。
在第67行你正在做:
ll_details.addView(ll_info);
尝试将ll_info添加到ll_details。但是在第39行,您已经将ll_info添加到主视图中:
main.addView(ll_info);
您无法将同一个视图对象添加到两个不同的父级。