2016-04-03 3 views
1

Я пытаюсь динамически раздувать второй XML-файл в кармане, но он вызывает исключение null-указателя. Когда я пытаюсь выполнить жесткий код (включив непосредственно внутри макета карты), он работает хорошо Есть ли другой способ раздуть новый XML динамически?Как надуть новый макет внутри карточного динамика динамически

CardView Layout

<?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 перед накачиванием его на здесь:

layout= (LinearLayout) parent.findViewById(R.id.room_detail_Linear_layout); 

Вот почему макет null.

Используйте onCreateViewHolder, чтобы вернуть экземпляр 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)); 
} 

, а затем в вашем ViewHolder классе:

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 к но это не сработало для меня, вот почему я пытался раздуть ole layout внутри cardView. –

+0

'onBindViewHolder' будет работать. Если вы не используете его, то 'RecyclerView' не будет работать должным образом. BTW 'onCreateViewHolder' вызывается только один раз, поэтому данные не будут обновляться. –

+0

onBindViewHolder работает хорошо по моей потребности. –

Смежные вопросы