如何调整基本适配器中的自定义布局

时间:2015-10-27 07:49:34

标签: android

我正在尝试获取线性布局并将其添加到自定义列表视图中。但它给了我错误。 "指定的孩子已经有父母。您必须先在孩子的父母身上调用removeView()"

    public View getView(int position, View convertView, ViewGroup parent) {

     LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View rowView = inflater.inflate(R.layout.emoji_chat_custom_list, null);
     Emoji_chat_list_item item = dataSet.get(position);
     LinearLayout linear = (LinearLayout) rowView.findViewById(R.id.linear);
     // ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
     // TextView textView = (TextView) rowView.findViewById(R.id.name);
     // imageView.setImageResource(item.getId());
     //textView.setText(item.getName());
     //linear.addView(linear);
     //  linear =     (LinearLayout)convertView.findViewById(R.id.inhorizontalscrollview);
     linear.addView(item.getlayout());
     //       return rowView;
       return rowView ;
}

这是自定义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="match_parent"
 android:orientation="horizontal"
 android:id="@+id/linear">
</LinearLayout>

这是主要的xml文件

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="{relativePackage}.${activityClass}" >
    <TableRow>
        <ListView
            android:id="@+id/list"

            android:layout_width="match_parent"
            android:layout_height="380dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
        </ListView>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"
            android:id="@+id/send"
            android:layout_column="2" />
    </TableRow>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="30dp"

        android:id="@+id/inhorizontalscrollview"
        android:layout_below="@+id/addinhorizontalscrollview"></LinearLayout>
    <TableRow>
    </TableRow>


    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="0">

            <TableRow android:layout_column="0">

                <ImageView
                    android:id="@+id/imageview1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/a"
                    />
                <ImageView
                    android:id="@+id/imageview2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ab"
                    />
                <ImageView
                    android:id="@+id/imageview3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/abc"
                    />
                <ImageView
                    android:id="@+id/imageview4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/abcd"
                    />


            </TableRow>
        </TableRow>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </TableRow>
</TableLayout>

我想要做的是我想选择图像并将其置于线性布局中。我已经完成了..但下一个目标是我想采用该布局并将其放在自定义列表视图中。

10-27 14:49:52.692 17418-17418/com.example.zeus_technologies.setchat D/dalvikvm: Late-enabling CheckJNI
10-27 14:49:52.757 17418-17418/com.example.zeus_technologies.setchat E/Trace: error opening trace file: No such file or directory (2)
10-27 14:49:52.832 17418-17418/com.example.zeus_technologies.setchat E/PhonePolicy: Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
10-27 14:49:52.877 17418-17418/com.example.zeus_technologies.setchat D/dalvikvm: GC_FOR_ALLOC freed 150K, 80% free 713K/3484K, paused 5ms, total 5ms
10-27 14:49:52.877 17418-17418/com.example.zeus_technologies.setchat I/dalvikvm-heap: Grow heap (frag case) to 4.291MB for 1536016-byte allocation
10-27 14:49:52.897 17418-17429/com.example.zeus_technologies.setchat D/dalvikvm: GC_FOR_ALLOC freed 1K, 56% free 2212K/5023K, paused 21ms, total 21ms
10-27 14:49:53.052 17418-17418/com.example.zeus_technologies.setchat D/libEGL: loaded /system/lib/egl/libEGL_mali.so
10-27 14:49:53.052 17418-17418/com.example.zeus_technologies.setchat D/libEGL: loaded /system/lib/egl/libGLESv1_CM_mali.so
10-27 14:49:53.062 17418-17418/com.example.zeus_technologies.setchat D/libEGL: loaded /system/lib/egl/libGLESv2_mali.so
10-27 14:49:53.087 17418-17418/com.example.zeus_technologies.setchat D/OpenGLRenderer: Enabling debug mode 0
10-27 14:49:55.131 17418-17418/com.example.zeus_technologies.setchat D/TextLayoutCache: Using debug level = 0 - Debug Enabled = 0
10-27 14:49:57.002 17418-17418/com.example.zeus_technologies.setchat I/dalvikvm: Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
10-27 14:49:57.002 17418-17418/com.example.zeus_technologies.setchat W/dalvikvm: VFY: unable to resolve virtual method 17797: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
10-27 14:49:57.002 17418-17418/com.example.zeus_technologies.setchat D/dalvikvm: VFY: replacing opcode 0x6f at 0x0007
10-27 14:49:57.007 17418-17418/com.example.zeus_technologies.setchat I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
10-27 14:49:57.012 17418-17418/com.example.zeus_technologies.setchat W/dalvikvm: VFY: unable to resolve virtual method 425: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
10-27 14:49:57.012 17418-17418/com.example.zeus_technologies.setchat D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
10-27 14:49:57.012 17418-17418/com.example.zeus_technologies.setchat I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
10-27 14:49:57.012 17418-17418/com.example.zeus_technologies.setchat W/dalvikvm: VFY: unable to resolve virtual method 447: Landroid/content/res/TypedArray;.getType (I)I
10-27 14:49:57.012 17418-17418/com.example.zeus_technologies.setchat D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
10-27 14:49:57.022 17418-17418/com.example.zeus_technologies.setchat I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
10-27 14:49:57.022 17418-17418/com.example.zeus_technologies.setchat W/dalvikvm: VFY: unable to resolve direct method 18415: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
10-27 14:49:57.022 17418-17418/com.example.zeus_technologies.setchat D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
10-27 14:49:57.022 17418-17418/com.example.zeus_technologies.setchat I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
10-27 14:49:57.022 17418-17418/com.example.zeus_technologies.setchat W/dalvikvm: VFY: unable to resolve direct method 18415: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
10-27 14:49:57.022 17418-17418/com.example.zeus_technologies.setchat D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
10-27 14:49:57.117 17418-17418/com.example.zeus_technologies.setchat D/dalvikvm: GC_FOR_ALLOC freed 1811K, 67% free 1986K/5983K, paused 9ms, total 9ms
10-27 14:49:59.305 17418-17418/com.example.zeus_technologies.setchat D/AndroidRuntime: Shutting down VM
10-27 14:49:59.305 17418-17418/com.example.zeus_technologies.setchat W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40d09300)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime: FATAL EXCEPTION: main
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.addViewInner(ViewGroup.java:3381)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.addView(ViewGroup.java:3252)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.addView(ViewGroup.java:3197)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.addView(ViewGroup.java:3173)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at com.example.zeus_technologies.setchat.CustomListAdapter.getView(CustomListAdapter.java:70)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.AbsListView.obtainView(AbsListView.java:2270)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.ListView.onMeasure(ListView.java:1128)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.TableRow.getColumnsWidths(TableRow.java:312)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.TableLayout.findLargestCells(TableLayout.java:504)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.TableLayout.measureVertical(TableLayout.java:469)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.TableLayout.onMeasure(TableLayout.java:435)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4817)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4817)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4817)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4817)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4817)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4817)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4817)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:78)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1109)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:675)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4817)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4817)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4817)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1875)
10-27 14:49:59.426 17418-17418/com.example.zeus_technologies.setchat E/AndroidRuntime:  at android.view.ViewR

1 个答案:

答案 0 :(得分:0)

只有当您向父布局添加了视图并尝试在Java代码中动态添加其他视图时,才会显示此错误,然后它应该在添加新Views之前删除您的视图。

如果您的emoji_chat_custom_list.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="match_parent"
 android:orientation="horizontal"
 android:id="@+id/linear">
</LinearLayout>

然后你不需要调用这个

linear.addView(item.getlayout());

如果不是,则调用

linear.removeAllViews()

之前

linear.addView(item.getlayout());

相关问题