AddView到ListView的java.lang.IllegalStateException

时间:2014-07-05 18:03:04

标签: android listview adapter

我尝试添加视图并根据我的需要从我的ListView适配器删除视图,通过调用:

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

 holder.wrapper.addView(holder.PimageView);

取决于我的需要,当convertView为null(第一次加载)时它可以找到,但是当我刷新它(ConvertView not null)时我得到了这个错误:

 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);

这些方法是Error发生的地方,我已经尝试过View.GONE和View.VISIBLE,但滚动后listview被混淆了,当它应该是GONE时,将版面空间保留为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;

  }
抱歉,如果它反复,但有些身体确切地问了

任何帮助家伙有什么不对?非常感谢

1 个答案:

答案 0 :(得分:1)

您错误地实现了ViewHolder模式。由于您使用findViewById()获得的观看次数已经是LinearLayout的子级,因此您根本无需致电addView()