2014-07-05 42 views
0

我嘗試addView的和觀的根據我的需要從我的ListView適配器,通過調用remove:java.lang.IllegalStateException上AddingView到的ListView

holder.wrapper.removeView(holder.PimageView); 
holder.wrapper.removeView(holder.theMessage); 

holder.wrapper.addView(holder.PimageView); 

根據我的需要,當convertView爲空(他們第一次加載)它的作品找到,但當我刷新(轉換視圖不爲空)我得到這個錯誤:

07-05 14:00:36.077: E/AndroidRuntime(2019): FATAL EXCEPTION: main 
07-05 14:00:36.077: E/AndroidRuntime(2019): Process: com.lifemate.lmmessenger, PID: 
2019 
07-05 14:00:36.077: E/AndroidRuntime(2019): java.lang.IllegalStateException: The 
specified 
child already has a parent. You must call removeView() on the child's parent first. 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.view.ViewGroup.addViewInner(ViewGroup.java:3562) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.view.ViewGroup.addView(ViewGroup.java:3415) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.view.ViewGroup.addView(ViewGroup.java:3360) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.view.ViewGroup.addView(ViewGroup.java:3336) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
com.lifemate.lmmessenger.listviewengine.ChatAdapter.getView(ChatAdapter.java:249) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.widget.AbsListView.obtainView(AbsListView.java:2240) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.widget.ListView.makeAndAddView(ListView.java:1790) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.widget.ListView.fillUp(ListView.java:725) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.widget.ListView.layoutChildren(ListView.java:1611) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.widget.AbsListView.onLayout(AbsListView.java:2091) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.view.View.layout(View.java:14817) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.view.ViewGroup.layout(ViewGroup.java:4631) 
07-05 14:00:36.077: E/AndroidRuntime(2019): at 
android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) 

順便說一句最後一行說的是RelativeLayout.onLayout,但我的xml是帶有LinearLayout子元素的FrameLayout,這是正常的嗎?

這是我的代碼:

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder = null; 
    if (convertView == null) { 
     holder = new ViewHolder(); 
     convertView = mLayoutInflater.inflate(R.layout.listitem_discuss, null); 
     this.convertview=convertView; 



    System.out.println("ConverView null"); 

    holder.PimageView = (ImageView)convertView.findViewById(R.id.PimageView); 
    holder.wrapper = (LinearLayout) convertView.findViewById(R.id.wrapper); 
    holder.theMessage = (TextView) convertView.findViewById(R.id.comment); 
    holder.theName = (TextView) convertView.findViewById(R.id.MSGname); 
    holder.theImage = (ImageView)convertView.findViewById(R.id.MSGimage); 
    holder.lp = (FrameLayout.LayoutParams) holder.theName.getLayoutParams(); 

holder.paramsleft = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT); 

holder.paramsright = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.RIGHT); 


    convertView.setTag(holder); 

    }else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    String theType="Known"; 

if(holder.badge!=null){ 
    holder.badge.setVisibility(View.GONE); 
    holder.badge.invalidate(); 
    holder.badge=null; 
} 

    mCursor.moveToPosition(position); 
    String imagenamer= 
(mCursor.getString(mCursor.getColumnIndex("username")).split("\\@"))[0]; 
    int isright= Integer.valueOf(mCursor.getString(mCursor.getColumnIndex("isright"))); 
    holder.wrapper.removeView(holder.PimageView); 
    holder.wrapper.removeView(holder.theMessage); 
    // here both DependantViews are removed 

    //Condition 1 : 

    holder.wrapper.addView(holder.PimageView); 

    //Condition 2 : 

    holder.wrapper.addView(holder.theMessage); 

這些方法是哪裏出錯一切發生的時候,我已經試過View.GONE和View.VISIBLE,但滾動列表視圖後,得到混合和葉保留的佈局空間當它應該是GONE時,比如當你調用View,INVISIBLE時,PImageView是空的。

這裏是我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" > 

<FrameLayout 
android:id="@+id/container" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
> 

    <TextView 
     android:id="@+id/MSGname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     /> 

    <ImageView 
     android:id="@+id/MSGimage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 

     /> 

<LinearLayout 
    android:id="@+id/wrapper" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/PimageView" 
     android:layout_width="1dp" 
     android:layout_height="1dp" 
     android:layout_margin="5dip" 
     /> 



    <TextView 
     android:id="@+id/comment" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dip" 
     android:background="@drawable/bubble_yellow" 
     android:textColor="@android:color/primary_text_light" /> 

</LinearLayout> 

</FrameLayout> 

</LinearLayout> 

持有人:

if (convertView == null) { 
     holder = new ViewHolder(); 
     convertView = mLayoutInflater.inflate(R.layout.listitem_discuss, null); 
     this.convertview=convertView; 



    System.out.println("ConverView null"); 

    holder.PimageView = (ImageView)convertView.findViewById(R.id.PimageView); 
    holder.wrapper = (LinearLayout) convertView.findViewById(R.id.wrapper); 
    holder.theMessage = (TextView) convertView.findViewById(R.id.comment); 
    holder.theName = (TextView) convertView.findViewById(R.id.MSGname); 
    holder.theImage = (ImageView)convertView.findViewById(R.id.MSGimage); 
    holder.lp = (FrameLayout.LayoutParams) holder.theName.getLayoutParams(); 
    holder.paramsleft = new FrameLayout.LayoutParams 
    (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT); 
    holder.paramsright = new FrameLayout.LayoutParams 
    (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.RIGHT); 


    convertView.setTag(holder); 

    }else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

和Viewholder:

public static class ViewHolder { 

    private TextView theMessage; 

    private TextView theName; 

    private LinearLayout wrapper;  

    private ImageView PimageView; 

    ImageView theImage; 

    ImageView theImage2; 

    FrameLayout.LayoutParams lp; 

    FrameLayout.LayoutParams paramsleft; 

    FrameLayout.LayoutParams paramsright; 

    BadgeView badge ; 

    VideoView PvideoView; 

    } 

對不起,如果它的重複,但一些機構正是問

任何幫助球員出了什麼問題?非常感謝

+0

ChatAdapter.java第249行在哪裏? –

+0

我提到過,它的\t \t holder.wrapper.addView(holder.theMessage); – user3805809

+0

它看起來像你試圖實現視圖模式,但沒有做到正確。 –

回答

1

您錯誤地實現了ViewHolder模式。由於您從findViewById()獲得的視圖已經是LinearLayout的子視圖,因此根本不需要撥打addView()

+0

男人,正如我所解釋的,我需要刪除並添加視圖的需要,我刪除視圖的前添加它們,我的問題是爲什麼這是發生在視圖被刪除時,你認爲我是一個阻滯? :D – user3805809

+0

@ user3805809你爲什麼要刪除視圖? –

+0

因爲我有我的聊天應用程序的列表視圖,有時我需要顯示imageView而不是textView,反之亦然 – user3805809

相關問題