我有这个 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="20dp" />
<Button
style="@style/ButtonText"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:enabled="false" />
</LinearLayout>
我正在尝试使用此模板并将其设置为循环以使其动态复制。我能够做到这一点。但是我需要为每个循环的子视图(Button
和)设置一些属性。TextView
到目前为止,这是我的代码:
int button_id = 0;
for (final CriticalElement e : skill.elements) {
// add LinearLayout row
LinearLayout LL = (LinearLayout) getActivity().getLayoutInflater()
.inflate(R.layout.motorskills_button_template, null);
llElementContainer.addView(LL);
final TextView tv = (TextView) ((ViewGroup) LL).getChildAt(0);
tv.setText(e.element);
Button b = (Button) ((ViewGroup) LL).getChildAt(1);
String tag = String.valueOf(e.element_num);
b.setTag(tag);
b.setId(button_id);
b.setEnabled(false);
LL.addView(b); // issue at this line
LL.addView(tv);
button_id++;
}
但是,我得到了这个堆栈跟踪:
04-03 08:42:10.326: E/AndroidRuntime(19019): FATAL EXCEPTION: main
04-03 08:42:10.326: E/AndroidRuntime(19019): Process: org.---.---, PID: 19019
04-03 08:42:10.326: E/AndroidRuntime(19019): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.view.ViewGroup.addView(ViewGroup.java:3415)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.view.ViewGroup.addView(ViewGroup.java:3360)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.view.ViewGroup.addView(ViewGroup.java:3336)
04-03 08:42:10.326: E/AndroidRuntime(19019): at org.---.---.SessionRecordMotorskillsFragment.onActivityCreated(SessionRecordMotorskillsFragment.java:125)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.os.Handler.handleCallback(Handler.java:733)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.os.Handler.dispatchMessage(Handler.java:95)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.os.Looper.loop(Looper.java:136)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 08:42:10.326: E/AndroidRuntime(19019): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 08:42:10.326: E/AndroidRuntime(19019): at java.lang.reflect.Method.invoke(Method.java:515)
04-03 08:42:10.326: E/AndroidRuntime(19019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 08:42:10.326: E/AndroidRuntime(19019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 08:42:10.326: E/AndroidRuntime(19019): at dalvik.system.NativeStart.main(Native Method)