我正在尝试使用 FirebaseRecyclerAdapter 填充 RecyclerViews 。我正在使用 FragmnetPagerAdapter 来填充并使用包含 RecyclerView 的ListFragment进行三个标签布局。我已经满足了所有常见问题,例如模型类中没有空构造函数,使用上下文e.t.c设置布局管理器。但是在调用protected void populateViewHolder()
函数时,我仍然得到一个空指针异常。
08-31 13:19:42.681 21993-21993/com.momskitchen.momskitchen V/MealListFragment:: mealname :Pakore
08-31 13:19:42.682 21993-21993/com.momskitchen.momskitchen E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.momskitchen.momskitchen.MealListFragment$3.populateViewHolder(MealListFragment.java:114)
at com.momskitchen.momskitchen.MealListFragment$3.populateViewHolder(MealListFragment.java:110)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:177)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5822)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5855)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5091)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4967)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1549)
at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:150)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:305)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是我的模型类
public class MealItem{
public String id;
public String name;
public Long pricePerUnit;
public MealItem() {
}
public MealItem(String id, String name, Long pricePerUnit) {
this.id = id;
this.name = name;
this.pricePerUnit = pricePerUnit;
}
}
这是我的ViewHolder类
public class MealHolder extends RecyclerView.ViewHolder {
View mView;
public final TextView mNameTV;
public final TextView mPriceTV;
public MealHolder(View itemView) {
super(itemView);
mView = itemView;
this.mNameTV = (TextView) mView.findViewById(R.id.meal_name_edit_text);
this.mPriceTV = (TextView) mView.findViewById(R.id.meal_price_text_view);
}
@Override
public String toString() {
return super.toString() + " '" + mNameTV.getText() + "'";
}
}
这是我尝试填充RecyclerView
的地方@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_meal_list, container, false);
int position = getArguments().getInt(ARG_POSITON);
// Set the adapter
if (view instanceof RecyclerView) {
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(getActivity().getApplicationContext(), mColumnCount));
}
recyclerView.setHasFixedSize(true);
if(position == 0){
lunchListAdapter = new FirebaseRecyclerAdapter<MealItem, MealHolder>(MealItem.class,R.layout.meal_item_layout_admin,MealHolder.class, FirebaseDatabase.getInstance().getReference().child("Meals").child("lunch")) {
@Override
protected void populateViewHolder(MealHolder viewHolder, MealItem model, int position) {
Log.v(TAG, "mealname :"+model.name);
viewHolder.mPriceTV.setText(model.name);
viewHolder.mNameTV.setText(""+model.pricePerUnit);
}
};
recyclerView.setAdapter(lunchListAdapter);
}else if(position == 1){
dessertListAdapter = new FirebaseRecyclerAdapter<MealItem, MealHolder>(MealItem.class,R.layout.meal_item_layout_admin,MealHolder.class, FirebaseDatabase.getInstance().getReference().child("Meals").child("dessert")) {
@Override
protected void populateViewHolder(MealHolder viewHolder, MealItem model, int position) {
Log.v(TAG, "mealname :"+model.name);
viewHolder.mNameTV.setText(model.name);
viewHolder.mPriceTV.setText(""+model.pricePerUnit);
}
};
recyclerView.setAdapter(dessertListAdapter);
}else if(position == 2){
complimentListAdapter = new FirebaseRecyclerAdapter<MealItem, MealHolder>(MealItem.class,R.layout.meal_item_layout_admin,MealHolder.class, FirebaseDatabase.getInstance().getReference().child("Meals").child("compliment")) {
@Override
protected void populateViewHolder(MealHolder viewHolder, MealItem model, int position) {
Log.v(TAG, "mealname :"+model.name);
viewHolder.mNameTV.setText(model.name);
viewHolder.mPriceTV.setText(""+model.pricePerUnit);
}
};
recyclerView.setAdapter(complimentListAdapter);
}
}
return view;
}
此处还有我的RecyclerView xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 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:id="@+id/list"
android:name="com.momskitchen.momskitchen.MealListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="com.momskitchen.momskitchen.MealListFragment"
tools:listitem="@layout/meal_item_layout_admin" />
这是我的列表项布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/meal_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/meal_price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>