0

我正在尝试将 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)

我不知道如何解决这个问题。请帮助我。在此先感谢。

4

5 回答 5

3

您需要TextView l_tempText在循环内重新创建。您不能使用相同的实例,更改文本并添加它,它必须是一个全新的对象。

将所有l_tempText代码移动到循环中,它应该可以工作。

于 2012-12-21T14:51:13.087 回答
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);
}
于 2012-12-21T14:50:02.403 回答
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);
    }
于 2012-12-21T14:55:46.823 回答
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您在第二个布局中忘记了参数

于 2012-12-21T14:49:41.930 回答
0

你得到异常,因为你正在删除 LinearLayout

((ViewGroup)sideIndex.getParent()).removeView(sideIndex);

然后尝试向同一个 LinearLayout 添加一些东西

sideIndex.addView(l_tempText);

在循环中一遍又一遍地使用相同的 TextView 实例。

于 2012-12-21T14:51:19.950 回答