这应该是一项简单的工作。我刚从api调用中获取数据,并希望在recyclerview中填充它。我做过这样的事情就像我猜的每个人一样。但是,当视图项布局在适配器类中膨胀时,我得到空指针异常。我不知道如何解决它。
我的适配器类是
public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> {
private List<RecipeByIngredients> recipeByIngredients;
private Context mContext;
public RecipeAdapter(List<RecipeByIngredients> recipeByIngredients, Context mContext) {
this.recipeByIngredients = recipeByIngredients;
this.mContext = mContext;
}
@Override
public RecipeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
final View itemView = mInflater.inflate(R.layout.recipe_item_layout, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
RecipeByIngredients recipeByIngredient = recipeByIngredients.get(position);
holder.tvUsedIngredients.setText(recipeByIngredient.getUsedIngredientCount() + " " + "ingredients used");
holder.tvMissedIngredients.setText(recipeByIngredient.getMissedIngredientCount() + " " + "missed ingredients");
Glide.with(mContext)
.load(recipeByIngredient.getImage())
.placeholder(R.drawable.recipe_placeholder)
.error(R.drawable.recipe_placeholder)
.into(holder.mainRecipePhoto);
}
@Override
public int getItemCount() {
return recipeByIngredients.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView mainRecipePhoto;
public TextView tvUsedIngredients;
public TextView tvMissedIngredients;
public ViewHolder(View itemView) {
super(itemView);
mainRecipePhoto = (ImageView) itemView.findViewById(R.id.ivRecipe);
tvMissedIngredients = (TextView) itemView.findViewById(R.id.tvMissedIngredients);
tvUsedIngredients = (TextView) itemView.findViewById(R.id.tvUsedIngredients);
}
}
}
项目布局xml是
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:autofit="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="200dp">
<ImageView
android:id="@+id/ivRecipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="50"
android:scaleType="centerCrop" />
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:padding="8dp"
android:text="Recipe name"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="@dimen/text_size_large"
autofit:minTextSize="10sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:weightSum="2">
<me.grantland.widget.AutofitTextView
android:id="@+id/tvUsedIngredients"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="8dp"
android:text="5 ingredients used"
android:textAllCaps="false"
android:textColor="#b7b7b7"
android:textSize="@dimen/text_size_small"
android:textStyle="bold|italic"
autofit:minTextSize="8sp" />
<view
android:layout_width="0.5dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#ffffff" />
<me.grantland.widget.AutofitTextView
android:id="@+id/tvMissedIngredients"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="8dp"
android:text="3 ingredients missed"
android:textAllCaps="false"
android:textColor="#b7b7b7"
android:textSize="@dimen/text_size_small"
android:textStyle="bold|italic"
autofit:minTextSize="8sp" />
</LinearLayout>
</FrameLayout>
我收到错误
java.lang.NullPointerException: Attempt to invoke virtual method
'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:715)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at projects.startup.foodmaker.appModules.ingredientSearch.RecipeAdapter.onCreateViewHolder(RecipeAdapter.java:36)
at projects.startup.foodmaker.appModules.ingredientSearch.RecipeAdapter.onCreateViewHolder(RecipeAdapter.java:22)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6290)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5478)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3254)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
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:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
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:15596)
答案 0 :(得分:6)
在layout.xml中将<view
更改为<View
答案 1 :(得分:0)
试试这个
将您的onCreateViewHolder()
方法更改为
@Override
public RecipeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item_layout,parent,false);
ViewHolder rcv = new ViewHolder(layoutView,mContext);
return rcv;
}