我正在尝试将 textview 作为子项添加到线性布局但出现异常。这是 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/buttonswoodytexturebg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/OkButton"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ok" />
<Button
android:id="@+id/AddContactButton"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add new Contact" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/listViewSetRule"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="2dip"
android:cacheColorHint="#00000000"
android:choiceMode="multipleChoice" />
<!-- android:smoothScrollbar="false" -->
<ListView
android:id="@+id/listViewUpdateRule"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="2dip"
android:cacheColorHint="#00000000"
android:choiceMode="multipleChoice" />
<LinearLayout
android:id="@+id/sideIndex"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:background="@color/black"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
在这里,我正在尝试sideIndex
使用以下代码将子级添加到具有 id 的线性布局中:
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
LinearLayout sideIndex = (LinearLayout)ShowTheContacts1.this.findViewById(R.id.sideIndex);
sideIndexHeight = sideIndex.getHeight();
((ViewGroup)sideIndex.getParent()).removeView(sideIndex);
TextView l_tempText = new TextView(ShowTheContacts1.this);
l_tempText.setGravity(Gravity.CENTER);
l_tempText.setTextSize(20);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
l_tempText.setLayoutParams(params);
for(int l_a = 0;l_a < m_arrayOfAlphabets.length;l_a++)
{
l_tempText.setText(m_arrayOfAlphabets[l_a]);
sideIndex.addView(l_tempText);
}
}
我在这一行遇到异常: sideIndex.addView(l_tempText);
这是 logcat :
12-21 20:08:06.291: E/AndroidRuntime(19007): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-21 20:08:06.291: E/AndroidRuntime(19007): at android.view.ViewGroup.addViewInner(ViewGroup.java:1861)
12-21 20:08:06.291: E/AndroidRuntime(19007): at android.view.ViewGroup.addView(ViewGroup.java:1756)
12-21 20:08:06.291: E/AndroidRuntime(19007): at android.view.ViewGroup.addView(ViewGroup.java:1713)
12-21 20:08:06.291: E/AndroidRuntime(19007): at android.view.ViewGroup.addView(ViewGroup.java:1693)
12-21 20:08:06.291: E/AndroidRuntime(19007): at com.velosys.smsManager.Activities.ShowTheContacts1.onWindowFocusChanged(ShowTheContacts1.java:1335)
12-21 20:08:06.291: E/AndroidRuntime(19007): at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:1969)
12-21 20:08:06.291: E/AndroidRuntime(19007): at android.view.View.dispatchWindowFocusChanged(View.java:3731)
12-21 20:08:06.291: E/AndroidRuntime(19007): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:657)
12-21 20:08:06.291: E/AndroidRuntime(19007): at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
12-21 20:08:06.291: E/AndroidRuntime(19007): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 20:08:06.291: E/AndroidRuntime(19007): at android.os.Looper.loop(Looper.java:123)
12-21 20:08:06.291: E/AndroidRuntime(19007): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-21 20:08:06.291: E/AndroidRuntime(19007): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 20:08:06.291: E/AndroidRuntime(19007): at java.lang.reflect.Method.invoke(Method.java:521)
12-21 20:08:06.291: E/AndroidRuntime(19007): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-21 20:08:06.291: E/AndroidRuntime(19007): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-21 20:08:06.291: E/AndroidRuntime(19007): at dalvik.system.NativeStart.main(Native Method)
我不知道如何解决这个问题。请帮助我。在此先感谢。