Android 如何缓解指定的子级已存在父级(IllegalStateException)错误?

Android 如何缓解指定的子级已存在父级(IllegalStateException)错误?,android,android-layout,illegalstateexception,Android,Android Layout,Illegalstateexception,起初,我试图重新调整我的线性布局,并以编程的方式查看这个问题 这会引发另一个异常,特别是IllegalStateException(表示“指定的子项已经有父项”) 此部分(第二条if语句)中的适配器中出现了错误: 这就是适配器在其根视图中的使用方式(我怀疑这可能是罪魁祸首,但我不确定该怎么办): 这段代码位于AsyncTask的onPostExecute()中 结果是一个ArrayList 用于(聊天:结果){ if(chat.senderId!=mAccount.accId){ add(新的

起初,我试图重新调整我的线性布局,并以编程的方式查看这个问题

这会引发另一个异常,特别是IllegalStateException(表示“指定的子项已经有父项”)

此部分(第二条if语句)中的适配器中出现了错误:

这就是适配器在其根视图中的使用方式(我怀疑这可能是罪魁祸首,但我不确定该怎么办):

这段代码位于AsyncTask的onPostExecute()中

结果是一个ArrayList
用于(聊天:结果){
if(chat.senderId!=mAccount.accId){
add(新的聊天字符串(false,chat.chatMessage));
}否则{
add(新的聊天字符串(true,chat.chatMessage));
}
}
简要说明此适配器的功能:

适配器假设向根视图中的listview添加新的“聊天泡泡”

有人能帮我吗

编辑1 我尝试了以下解决方案(仍然没有成功):

//删除父级。
如果(chat.mLeft){
text chat.setText(chat.mText);
}否则{
视图=(视图)包装器.getParent();
wrapper.removeView(视图);
包装器。移除视图(1);
wrapper.addView(thumbPhoto,0);
text chat.setText(chat.mText);
}
if(行==null){
LayoutFlater充气器=(LayoutFlater)this.getContext().getSystemService(Context.LAYOUT\u充气器\u服务);

行=充气机。充气(R.layout.chat_项,空);您可以编辑getView如下:

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ChatString chat = getItem(position);
    if (row == null) {
         LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         // Here, inflate 2 different layouts
         if(chat.mLeft){
             row = inflater.inflate(R.layout.chat_item, parent, false);
         } else {
             row = inflater.inflate(R.layout.chat_item_right, parent, false);
         }
    }

    wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

    textChat = (TextView) row.findViewById(R.id.textMessage);
    thumbPhoto = (ImageView) row.findViewById(R.id.thumbPhoto);

    // Here, just set the text
    textChat.setText(chat.mText);
    return row;
}
要指示视图循环以提供一致的视图,请向适配器中添加:

@Override
public int getItemViewType(int position) {
    ChatString chat = getItem(position);
    if (chat.mLeft) {
        return 0;
    } else {
        return 1;
    }
}


wrapper.addView(thumbPhoto,0)
thumbPhoto已经有了一个父级,因为它在第行中。您要做的是有两种不同的布局,并使用getViewTypeCount和getItemViewType声明两种不同的类型。@njzk2您有什么建议?您能提供一个代码示例吗?它将告诉视图回收过程仅对相同的视图类型回收视图。谢谢。。。谢谢你…谢谢你…它能工作…我不知道有像getItemViewType和getViewTypeCount这样的东西。。。。
// Removing the parent.
 if(chat.mLeft){
     textChat.setText(chat.mText); 
 }else{
     View view = (View) wrapper.getParent();
     wrapper.removeView(view);
     wrapper.removeViewAt(1);
     wrapper.addView(thumbPhoto, 0);
     textChat.setText(chat.mText);
 }

 if (row == null) {
         LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         row = inflater.inflate(R.layout.chat_item, null); <---- CHANGING THIS BIT
 }
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ChatString chat = getItem(position);
    if (row == null) {
         LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         // Here, inflate 2 different layouts
         if(chat.mLeft){
             row = inflater.inflate(R.layout.chat_item, parent, false);
         } else {
             row = inflater.inflate(R.layout.chat_item_right, parent, false);
         }
    }

    wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

    textChat = (TextView) row.findViewById(R.id.textMessage);
    thumbPhoto = (ImageView) row.findViewById(R.id.thumbPhoto);

    // Here, just set the text
    textChat.setText(chat.mText);
    return row;
}
@Override
public int getItemViewType(int position) {
    ChatString chat = getItem(position);
    if (chat.mLeft) {
        return 0;
    } else {
        return 1;
    }
}
@Override
public int getViewTypeCount() {
    return 2;
}