2015-10-27 37 views
0

我正在嘗試獲取線性佈局並將其添加到自定義列表視圖中。但它給了我錯誤。 「指定的孩子已經有一個家長,你必須首先對孩子的父母打電話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

之前調用

linear.removeAllViews()發佈你的logcat? –

+0

嘗試像這樣 - LinearLayout linear =(LinearLayout)rowView;我認爲不需要再看看。 – RajSharma

+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.addView(item.getlayout());

+0

錯誤已被刪除,但它仍然沒有約束與自定義列表視圖 –

+0

只需檢查自定義適配器教程 – Piyush