我在做什么:: 我正在动态地将xml添加到容器
发生了什么::
MainActivity.java
public class MainActivity extends Activity {
LinearLayout dynAddrId,rootContainer;
ImageView btnAddId,btnRemoveId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dynAddrId=(LinearLayout) findViewById(R.id.dynAddrId);
btnAddId=(ImageView) findViewById(R.id.btnAddId);
btnRemoveId=(ImageView) findViewById(R.id.btnRemoveId);
rootContainer=(LinearLayout) findViewById(R.id.rootContainer);
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
btnAddId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dynAddrId.addView(addView);
}
});
}
}
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="horizontal" >
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/languagesList" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/rootContainer"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/languagesList" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/btnAddId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
<ImageView
android:id="@+id/btnRemoveId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/dynAddrId" >
</LinearLayout>
</LinearLayout>
的日志::
12-10 11:24:16.767: E/AndroidRuntime(14953): FATAL EXCEPTION: main
12-10 11:24:16.767: E/AndroidRuntime(14953): Process: com.example.addaddressproof, PID: 14953
12-10 11:24:16.767: E/AndroidRuntime(14953): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-10 11:24:16.767: E/AndroidRuntime(14953): at android.view.ViewGroup.addViewInner(ViewGroup.java:3564)
12-10 11:24:16.767: E/AndroidRuntime(14953): at android.view.ViewGroup.addView(ViewGroup.java:3417)
12-10 11:24:16.767: E/AndroidRuntime(14953): at android.view.ViewGroup.addView(ViewGroup.java:3362)
12-10 11:24:16.767: E/AndroidRuntime(14953): at android.view.ViewGroup.addView(ViewGroup.java:3338)
12-10 11:24:16.767: E/AndroidRuntime(14953): at com.example.addaddressproof.MainActivity$1.onClick(MainActivity.java:43)
12-10 11:24:16.767: E/AndroidRuntime(14953): at android.view.View.performClick(View.java:4456)
12-10 11:24:16.767: E/AndroidRuntime(14953): at android.view.View$PerformClick.run(View.java:18465)
12-10 11:24:16.767: E/AndroidRuntime(14953): at android.os.Handler.handleCallback(Handler.java:733)
12-10 11:24:16.767: E/AndroidRuntime(14953): at android.os.Handler.dispatchMessage(Handler.java:95)
12-10 11:24:16.767: E/AndroidRuntime(14953): at android.os.Looper.loop(Looper.java:136)
12-10 11:24:16.767: E/AndroidRuntime(14953): at android.app.ActivityThread.main(ActivityThread.java:5086)
12-10 11:24:16.767: E/AndroidRuntime(14953): at java.lang.reflect.Method.invokeNative(Native Method)
12-10 11:24:16.767: E/AndroidRuntime(14953): at java.lang.reflect.Method.invoke(Method.java:515)
12-10 11:24:16.767: E/AndroidRuntime(14953): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-10 11:24:16.767: E/AndroidRuntime(14953): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-10 11:24:16.767: E/AndroidRuntime(14953): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
尝试在按钮onClick上添加View代码:
btnAddId.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View addView = LayoutInflater.from(MainActivity.this).inflate(R.layout.row,null);
dynAddrId.addView(addView);
}
});
注意:尝试使用ScrollView作为dynAddrId LinearLayout的父级,以便在屏幕上处理视图溢出。
答案 1 :(得分:0)
指定的孩子已经有父母。
错误表示您尝试在屏幕上显示的视图在屏幕上已经。
在您的情况下,您正在创建和存储视图addView
。当您第一次单击该按钮时,它将显示在屏幕上。当您下次单击时,您一定会收到错误。
根据评论中的建议,将初始化移至onClick()
。