shady shady - 3 months ago 12
Android Question

NullPointerException when adding header to heterogeneous RecyclerView

I receive this error while binding Views.

The code works fine when I do not add any header.

The Views are getting initialized properly and the data is getting displayed.

But Whenever I try to add the header, I get this exception.

I have initialized all the Views, but it can't just get the reference.

And the highlighted possible question does not solve the problem as I am binding the Views after passing the reference.

So I am not calling the Views before passing the reference.

FATAL EXCEPTION: main
Process: com.example.shashankdc.learn, PID: 8615
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.shashankdc.learn.Medals.Medals_Adapter.onBindViewHolder(Medals_Adapter.java:62)
at com.example.shashankdc.learn.Medals.Medals_Adapter.onBindViewHolder(Medals_Adapter.java:17)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5138)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4433)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1364)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:556)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2713)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3011)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1009)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:719)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:984)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:732)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2186)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1920)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:557)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
at android.os.Handler.handleCallback(Handler.java:739)


This is my Adapter

package com.example.shashankdc.learn.Medals;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.shashankdc.learn.R;

import java.util.List;


public class Medals_Adapter extends RecyclerView.Adapter<Medals_Adapter.MyHolder> {

private static final int TYPE_HEAD = 0;
private static final int TYPE_LIST = 1;
private List<Medals_Model> mylist;
private Context context;

public Medals_Adapter(List<Medals_Model> mylist, Context context) {
this.mylist = mylist;
this.context = context;
}

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

if (viewType == TYPE_LIST) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.medals_card, parent, false);
return new MyHolder(v, viewType);
} else if (viewType == TYPE_HEAD) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_img, parent, false);
return new MyHolder(v, viewType);
}

return null;
}

@Override
public void onBindViewHolder(MyHolder holder, int position) {


if (holder.view_type == TYPE_LIST) {

Medals_Model model = mylist.get(position + 1);
holder.name.setText(model.getName());
holder.requirements.setText(model.getRequirements());
holder.bronze.setText(String.valueOf(model.getBronze()));
holder.silver.setText(String.valueOf(model.getSilver()));
holder.gold.setText(String.valueOf(model.getGold()));

} else if (holder.view_type == TYPE_HEAD) {

holder.header.setText("HEADER");

}


}

@Override
public int getItemCount() {
return mylist.size() + 1;
}

public void add(Medals_Model model) {
mylist.add(model);
notifyDataSetChanged();
}

@Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_HEAD;
} else {
return TYPE_LIST;
}
}

public class MyHolder extends RecyclerView.ViewHolder {
public TextView name, requirements, gold, silver, bronze;
//Header
public TextView header;
int view_type;

public MyHolder(View itemView, int view_type) {
super(itemView);

if (view_type == TYPE_LIST) {
name = (TextView) itemView.findViewById(R.id.medals_title);
requirements = (TextView) itemView.findViewById(R.id.medals_requirements);
gold = (TextView) itemView.findViewById(R.id.medals_gold);
silver = (TextView) itemView.findViewById(R.id.medals_silver);
bronze = (TextView) itemView.findViewById(R.id.medals_bronze);
view_type = 1;
} else if (view_type == TYPE_HEAD) {
//Header_config
header = (TextView) itemView.findViewById(R.id.my_header);
view_type = 0;
}


}
}

}


Here is my header_img.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">

<android.support.v7.widget.CardView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
card_view:cardCornerRadius="3dp">

<TextView
android:layout_width="match_parent"
android:id="@+id/my_header"
android:layout_height="wrap_content" />

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


And the content_medals(Medal).xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.shashankdc.learn.Medals.Medals"
tools:showIn="@layout/medals">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView_medals"
android:background="@drawable/linearlayour_gradient"
/>
</RelativeLayout>

Answer

These lines are changing the parameter, which is shadowing the field.

view_type = 1;
view_type = 0;

Please update in the ViewHolder constructor to qualify the correct variable you want to set

public MyHolder(View itemView, int view_type) {
    super(itemView);

    this.view_type = view_type; // Do it here
    if (view_type == TYPE_LIST) {
        // Remove view_type = 1;
    } else if ( ... )
        // Remove view_type = 0;

Your error was that something similar to this condition falling through because holder.view_type was always 0.

if (holder.view_type == TYPE_LIST) {

    Medals_Model model = mylist.get(position + 1);

    // Error because holder.name is not defined for a header
    holder.name.setText(model.getName()); 

Alternatively, use getItemViewType(position) rather than rely on holder.view_type

Comments