我有一些关于膨胀和重新使用同一个 TextView 的问题。
就像它试图一遍又一遍地覆盖相同的文本视图或其他东西,但它不能这样做?
LayoutInflater inflater = LayoutInflater.from(this);
View mainlayout = inflater.inflate(R.layout.days_monday_inflate, null);
View layout_number = inflater.inflate(R.layout.inflate_number, null);
for (int i = 0; i < 10; i++) {
row = new TableRow(this);
number = (TextView) layout_number.findViewById(R.id.Number);
number.setTag(i);
number.setText(Integer.toString(i));
row.addView(number);
}
setContentView(mainlayout);
这是 inflate_number.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Number"
android:layout_width="3dp"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:text="1" />
这是我得到的错误,它在第 51 行,即:
row.addView(number);
07-18 20:54:25.124: E/AndroidRuntime(1166): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.view.ViewGroup.addView(ViewGroup.java:1865)
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.view.ViewGroup.addView(ViewGroup.java:1822)
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.view.ViewGroup.addView(ViewGroup.java:1802)
07-18 20:54:25.124: E/AndroidRuntime(1166): at com.trainingschedule.days.Monday.onCreate(Monday.java:50)
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 20:54:25.124: E/AndroidRuntime(1166): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)