【问题标题】:java.lang.IllegalStateException on AddingView to ListView将视图添加到 ListView 上的 java.lang.IllegalStateException
【发布时间】:2014-08-26 16:03:57
【问题描述】:

我试图根据我的需要从我的 ListView 适配器中添加视图和删除视图,方法是:

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

 holder.wrapper.addView(holder.PimageView);

根据我的需要,当 convertView 为空(第一次加载)时,它可以找到,但是当我刷新它(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,但是在滚动列表视图之后会混淆,并且当 PImageView 应该是 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;

  }

对不起,如果它重复了,但有人准确地问了

任何帮助家伙什么是错的?非常感谢

【问题讨论】:

  • ChatAdapter.java 的第 249 行在哪里?
  • 我提到了,它的 holder.wrapper.addView(holder.theMessage);
  • 看起来您正在尝试实现 View Holder 模式,但没有正确完成。
  • ViewHolder 的声明是什么?
  • 如果您要存储膨胀视图中的 LinearLayout,您不需要存储它的子元素也不需要添加它们,因为它们已经属于 LinearLayout。

标签: android listview adapter


【解决方案1】:

您错误地实现了 ViewHolder 模式。由于您使用findViewById() 获得的视图已经是LinearLayout 的子级,因此您根本不需要调用addView()

【讨论】:

  • 伙计,正如我所解释的,我需要根据需要删除和添加它们的视图,我在添加它们之前删除了两个视图,我的问题是为什么当视图被删除时会发生这种情况你的想法我是一个智障? :D
  • @user3805809 你为什么要删除视图?
  • 因为我的聊天应用有一个列表视图,所以有时我需要显示 imageView 而不是 textView,反之亦然
  • @user3805809 那么你应该有两个不同的布局,而不是试图操纵一个布局。
  • 嗯,谢谢,你能引导我找到一个正确的教程吗?因为我想出来的方法是为它们创建 2x ViewHolder 和 ConvertView,这似乎不正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-03
相关资源
最近更新 更多