2016-04-03 21 views
1

cardview内で2番目のXMLを動的に膨張させようとしていますが、nullポインタ例外がスローされます。(カードビューのレイアウトに直接インクルードして) 。新しいXMLを動的に拡張する他の方法はありませんか?動的にcardview内の新しいレイアウトを膨張させる方法

CardViewレイアウト

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:id="@+id/room_detail_cards" 
    android:layout_height="wrap_content"> 
    <LinearLayout 
     android:id="@+id/room_detail_Linear_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <include layout="@layout/module_type_5"/> 
     <!--<include layout="@layout/module_type_3"/>--> 
     <!--<It Works well when i hard code the layout here/>--> 

</LinearLayout> 
</android.support.v7.widget.CardView> 

RecyclerAdapter.java(onCreateViewHolder)

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    layout= (LinearLayout) parent.findViewById(R.id.room_detail_Linear_layout); 
    View itemView = LayoutInflater. 
       from(parent.getContext()). 
       inflate(R.layout.module_type_3, layout, false); 
    layout.addView(itemView);    //NULL Pointer Exception occurs here 
    View itemView1 = LayoutInflater. 
      from(parent.getContext()). 
      inflate(R.layout.room_detail_card_view, null, false); 

    return new ViewHolder(itemView1); 
} 

まずレイアウトXML(module_type_5)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:hint="module" 
     android:id="@+id/module_5" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <LinearLayout 
     android:padding="10dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:src="@drawable/add" 
      android:id="@+id/switch_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <ImageView 
      android:src="@drawable/add" 
      android:id="@+id/switch_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <ImageView 
      android:src="@drawable/add" 
      android:id="@+id/switch_3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <ImageView 
      android:src="@drawable/add" 
      android:id="@+id/switch_4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <ImageView 
      android:src="@drawable/add" 
      android:id="@+id/switch_5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

</LinearLayout> 

第XMLレイアウト(module_type_3)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:padding="5dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:hint="module" 
     android:id="@+id/module_5" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <LinearLayout 
     android:padding="10dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:src="@drawable/add" 
      android:id="@+id/switch_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <ImageView 
      android:src="@drawable/add" 
      android:id="@+id/switch_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <ImageView 
      android:src="@drawable/add" 
      android:id="@+id/switch_3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
</LinearLayout> 

    </LinearLayout> 

答えて

0

あなたは、ここでそれを膨らませる前にview idを取得しようとしている:

レイアウトは nullである理由です
layout= (LinearLayout) parent.findViewById(R.id.room_detail_Linear_layout); 

onCreateViewHolderを使用して、ViewHolderのインスタンスを返し、すべての割り当てをビュー所有者で実行します。

このような何かが働くだろう:

あなた ViewHolderクラスの
@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    return new ViewHolder(LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.room_detail_card_view, parent, false)); 
} 

そして:

public class ViewHolder extends RecyclerView.ViewHolder{ 

    TextView textView; 
    public ViewHolder(View itemView) { 
     super(itemView); 
     textView = (TextView)itemView.findViewById(R.id.textView); 
    } 
} 

そして最後にonBindViewHolderでデータバインディング:

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    holder.textView.setText("some data") 
} 
+0

まず私はonBindViewHolderのために試してみました画像を保持するが、それは私のために働かなかった、なぜ私はwhを膨らませようとしたレイアウトをcardView内に配置します。 –

+0

'onBindViewHolder'が動作します。あなたがそれを使用していないなら、 'RecyclerView'は正しく動作しません。 BTW onCreateViewHolderは一度だけ呼び出されるため、データは更新されません。 –

+0

onBindViewHolderは私の必要に応じてうまく機能します。 –

関連する問題