我正在尝试将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>
在这里,我尝试使用以下代码将子项添加到ID为sideIndex
的线性布局:
@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)
我不知道如何解决这个问题。请帮帮我。谢谢。
答案 0 :(得分:3)
您需要在循环中重新创建TextView l_tempText
。您不能使用相同的实例,更改文本并添加它,它必须是一个全新的对象。
将所有l_tempText
代码移到循环中,它应该可以正常工作。
答案 1 :(得分:2)
您正尝试多次添加同一个对象。
for(int l_a = 0;l_a < m_arrayOfAlphabets.length;l_a++)
{
l_tempText.setText(m_arrayOfAlphabets[l_a]);
sideIndex.addView(l_tempText);
}
每次都应该创建 l_tempText
:
for(int l_a = 0;l_a < m_arrayOfAlphabets.length;l_a++)
{
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);
l_tempText.setText(m_arrayOfAlphabets[l_a]);
sideIndex.addView(l_tempText);
}
答案 2 :(得分:2)
l_tempText应该在“for”语句
中创建 LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 1);
TextView l_tempText = null;
for(int l_a = 0;l_a < m_arrayOfAlphabets.length;l_a++)
{
l_tempText = new TextView(ShowTheContacts1.this);
l_tempText.setGravity(Gravity.CENTER);
l_tempText.setTextSize(20);
l_tempText.setLayoutParams(params);
l_tempText.setText(m_arrayOfAlphabets[l_a]);
sideIndex.addView(l_tempText);
}
答案 3 :(得分:0)
<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"
android:orientation="vertical" >
你在第二种布局中忘记了android:orientation
param
答案 4 :(得分:0)
由于您要删除LinearLayout
,因此会出现异常((ViewGroup)sideIndex.getParent()).removeView(sideIndex);
然后尝试向同一个LinearLayout添加内容
sideIndex.addView(l_tempText);
同样,TextView的相同实例在循环中反复使用。