2015-02-24 49 views
0

在我的應用程序中,我實現了註釋模塊,在用戶發佈註釋後,我將動態添加評論佈局。但我越來越

illegalstateException:指定的孩子已經haas父母。你必須首先調用子父對象的removeView()。java.lang.IllegalStateException:指定的子項已經有父項:On添加動態視圖

我膨脹了comment.xml並將其添加到commentsection視圖中。 的代碼是這樣的:

public void loadCommentUiFragment(List<UserComment> userCommentList){ 
     View commentView = null; 
     if(commentView != null){ 
      commentSection.removeView(commentView); 
     } 

     for(UserComment userComment : userCommentList){ 
      LayoutInflater inflator = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
      commentView = inflator.inflate(R.layout.comment_section, null); 
      ImageView userImageView = (ImageView) commentView.findViewById(R.id.user_image); 
      TextView userNameView = (TextView) commentView.findViewById(R.id.user_name); 
      TextView userTimeView = (TextView) commentView.findViewById(R.id.user_time); 
      TextView userCommentView = (TextView) commentView.findViewById(R.id.user_comments); 

      userNameView.setText(userComment.getUserName()); 
      userTimeView.setText(userComment.getCreatedAt()); 
      userCommentView.setText(userComment.getCommentText()); 

      commentSection.addView(commentView); 
     } 
     userCommentList.clear(); 
    } 

而且comment.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/post_comment_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:layout_marginBottom="10dip" 
     > 
    <LinearLayout 
     android:id="@+id/line_divider" 
     android:layout_width="match_parent" 
     android:layout_height="3dip" 
     android:layout_marginTop="5dip" 
     android:layout_marginBottom="10dip" 
     android:background="@color/layout_bg" 
     android:orientation="vertical"> 

    </LinearLayout> 

     <ImageView 
      android:id="@+id/user_image" 
      android:layout_width="40dip" 
      android:layout_height="40dip" 
      android:background="@color/layout_bg" 
      android:layout_below="@+id/line_divider" 
      /> 

     <TextView 
      android:id="@+id/user_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="user_name" 
      android:layout_toRightOf="@+id/user_image" 
      android:layout_marginLeft="10dip" 
      android:layout_marginTop="10dip" 
      android:textColor="#000000" 
      android:textStyle="bold"/> 

     <TextView 
      android:id="@+id/user_time" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Time of comments" 
      android:layout_toRightOf="@+id/user_name" 
      android:layout_marginLeft="80dip" 
      android:layout_marginTop="10dip" 
      android:textColor="#000000" 
      android:textStyle="bold" 
      android:maxLength="7"/> 

     <TextView 
      android:id="@+id/user_comments" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/user_name" 
      android:text="comments of the user" 
      android:layout_toRightOf="@+id/user_image" 
      android:layout_marginLeft="10dip" 
      android:layout_marginTop="5dip" 
      android:textColor="#000000" 
      android:textSize="16dip"/> 

    </RelativeLayout> 

main_activity.xml

<LinearLayout 
       android:id="@+id/commment_section" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dip" 
       android:orientation="vertical" 
       android:layout_below="@+id/nmd_user_img" 
       android:layout_marginBottom="40dip" 
       > 

       <RelativeLayout 
        android:id="@+id/pre_comment_layout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="#90C695" 
        > 

        <ImageView 
         android:id="@+id/cmt_user_image" 
         android:layout_width="30dip" 
         android:layout_height="30dip" 
         android:layout_margin="5dip" 
         android:padding="2dip" 
         android:background="@color/layout_bg"/> 
        <EditText 
         android:id="@+id/cmt_user_edt" 
         android:layout_width="240dip" 
         android:layout_height="wrap_content" 
         android:layout_toRightOf="@id/cmt_user_image" 
         android:layout_marginLeft="10dip" 
         android:padding="5dip" 
         android:hint="Enter your comments"/> 
        <LinearLayout 
         android:layout_width="100dip" 
         android:layout_height="wrap_content" 
         android:layout_below="@+id/cmt_user_image" 
         android:layout_marginTop="5dip" 
         android:gravity="center_horizontal" 
         android:layout_gravity="center" 
         > 
         <ImageButton 
          android:id="@+id/post_comment" 
          android:layout_width="90dip" 
          android:layout_height="40dip" 
          android:layout_marginTop="5dip" 
          android:layout_marginRight="15dip" 
          android:background="@drawable/post"/> 
        </LinearLayout> 
       </RelativeLayout> 

我沒有得到這個怎麼解決上述代碼的問題是,如果我不調用removeView()。它繼續添加前一個視圖以及新的加載視圖。

所以在的LinearLayout與id作爲commment_section我充氣comment.xml動態。

+0

你可以維護視圖的數組來添加以前的視圖,但是需要調用 – KOTIOS 2015-02-24 06:13:45

+0

來顯示你的分片代碼。 – 2015-02-24 06:20:07

+0

我沒有片段......基本上我有活動的主佈局,另一個佈局是comment.xml。在主要活動佈局評論部分是相對佈局,因爲我膨脹comment.xml佈局 – anand 2015-02-24 06:22:29

回答

2

上述代碼的問題是,如果我不調用removeView()。這是 不斷用新的加載視圖

要顯示不斷有新的視圖的佈局中使用removeAllViews代替removeView等一起加入以前的觀點:

if(commentSection != null){ 
    int childCount=commentSection.getChildCount(); 
    if(childCount>0) 
     commentSection.removeAllViews(); 
} 

IllegalStateException異常:指定的孩子已經哈斯父。你的 必須首先調用父視圖的removeView()。

commentSection添加commentView分配每個視圖一個新的ID:

你又添加
int view_id=2015; 
    for(UserComment userComment : userCommentList){ 
    LayoutInflater inflator=(LayoutInflater)getSystemService(
                 LAYOUT_INFLATER_SERVICE); 
    View commentView = inflator.inflate(R.layout.alertboxdialog, null); 
    commentView.setId(view_id); 
    view_id++; 
    //..... 
} 
+0

Thanx ...它工作正常...現在我解除了頭痛.... – anand 2015-02-24 07:02:54

1

同一視圖對象時,它可能會導致異常

一旦嘗試每個循環如下的時間採取新視圖

for(UserComment userComment : userCommentList){ 
     LayoutInflater inflator = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
     View commentView = inflator.inflate(R.layout.comment_section, null); 
     ............................. 
     ............................... 

     commentSection.addView(commentView); 
    } 

希望這會幫助你。

0

根據您的代碼,您並未嘗試在父視圖中添加相同的視圖(ID)。你必須創建每個新的View

它是全球性的在你的情況。嘗試把內部循環。

View commentView = null; [Wrong] 
for(....){ 
    LayoutInflater inflator = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
    View commentView = inflator.inflate(R.layout.comment_section, null); 
    commentSection.addView(commentView); 
    } 
相關問題