我在recyclerview
适配器中扩充我的布局xml。但我每次都遇到 NPE ,似乎无法找到问题的根源。
这是我的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)
.......

我的适配器:
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();
}
}
&#13;
我的 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>
&#13;
logcat表明这一行:
holder.mTextView.setText(m.getName());
我的异常发生的地方。
m.getName()
肯定不是空的,甚至不是问题,因为我尝试了holder.mTextView.setText("Test");
。
任何想法到底发生了什么?
答案 0 :(得分:1)
grid_cell.xml 中文本的ID是标题和金额,尝试使用其中一个而不是名称。像这样的东西
mTextView = (TextView) v.findViewById(R.id.title);
答案 1 :(得分:1)
问题是TextView = (TextView) v.findViewById(R.id.name); return null;
,因为在您的xml文件中没有带有ID名称的TextView
。有两种解决方案:使用textView
添加id="name"
或您可以使用
AnyTextView TextView = (AnyTextView) v.findViewById(R.id.title);
代替您初始化textview
。
答案 2 :(得分:1)
mTextView
为null,因为在grid_cell.xml中,您没有ID为name
的视图。尝试将作业更改为:
mTextView = (TextView) v.findViewById(R.id.title);
当然假设“标题”是你想要获得的观点。
[编辑] 道歉,米格尔在我做之前就到了! [/编辑]
答案 3 :(得分:1)
在您的xml文件中,您没有任何带有“name”ID的TextView
!如何在R.id.name
中使用findViewById
?您必须更改ViewHolder
课程:
mTextView = (TextView) v.findViewById(R.id.title);
OR
mTextView = (TextView) v.findViewById(R.id.amount);