Reputation: 2785
I am inflating my layout xml in a recyclerview
adapter. But i am running into an NPE everytime and cannot seem to locate the root of the problem.
Here's my logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.softtech.stevekamau.buyathome.adapter.RecomGridAdapter.onBindViewHolder(RecomGridAdapter.java:159)
at com.softtech.stevekamau.buyathome.adapter.RecomGridAdapter.onBindViewHolder(RecomGridAdapter.java:120)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
.......
And my adapter:
public class RecomGridAdapter extends RecyclerView.Adapter<RecomGridAdapter.ViewHolder> {
private List<NewModel> modelItems;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.name);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public RecomGridAdapter(List<NewModel> modelItems) {
this.modelItems = modelItems;
}
// Create new views (invoked by the layout manager)
@Override
public RecomGridAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.grid_cell, parent, false);
// set the view's size, margins, paddings and layout parameters
return new ViewHolder(v);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
NewModel m = modelItems.get(position);
holder.mTextView.setText(m.getName());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return modelItems.size();
}
}
And my grid_cell.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:foobar="http://schemas.android.com/apk/res-auto"
android:layout_width="118dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector"
card_view:cardCornerRadius="3dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="@drawable/loading4" />
<RelativeLayout
android:layout_width="110dp"
android:layout_height="60dp">
<ImageView
android:id="@+id/options"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:src="@drawable/options" />
<com.ctrlplusz.anytextview.AnyTextView
android:id="@+id/title"
android:layout_width="70dp"
android:layout_height="35dp"
android:ellipsize="end"
android:ems="10"
android:gravity="start"
android:maxLines="2"
android:paddingLeft="5dp"
android:text="Samsungf usb cablexvndvnsddvjdbv"
android:textSize="14dp"
android:textStyle="bold"
foobar:typeface="Gudea-Regular.ttf" />
<com.ctrlplusz.anytextview.AnyTextView
android:id="@+id/amount"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:ellipsize="end"
android:ems="10"
android:paddingLeft="5dp"
android:singleLine="true"
android:text="10000000000"
android:textColor="#cf5862"
android:textSize="18dp"
foobar:typeface="Gudea-Bold.ttf" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
The logcat suggests that this line:
holder.mTextView.setText(m.getName());
Is where my exception occurs.
And m.getName()
is definately not null or even the problem because i have tried holder.mTextView.setText("Test");
.
Any ideas what exactly is happening?
Upvotes: 0
Views: 4759
Reputation: 149
The problem is TextView = (TextView) v.findViewById(R.id.name); return null;
because in your xml file there is no TextView
with id name. There are 2 solutions: add a textView
with id="name"
or you can use
AnyTextView TextView = (AnyTextView) v.findViewById(R.id.title);
in place of your initialisation of your textview
.
Upvotes: 1
Reputation: 804
In your xml file, you don't have any TextView
with 'name' id!! How you use R.id.name
in findViewById
? You must change your ViewHolder
class:
mTextView = (TextView) v.findViewById(R.id.title);
OR
mTextView = (TextView) v.findViewById(R.id.amount);
Upvotes: 1
Reputation: 101
mTextView
is null, because in grid_cell.xml you don't have a view with the ID of name
. Try changing the assignment to:
mTextView = (TextView) v.findViewById(R.id.title);
Presuming of course that "title" is the view you intended to get.
[edit] Apologies, Miguel got there before I did! [/edit]
Upvotes: 1
Reputation: 2322
The ids of your texts in grid_cell.xml are title and amount, try to use one of them instead of name. Something like this
mTextView = (TextView) v.findViewById(R.id.title);
Upvotes: 1