gpt4 book ai didi

java - RecyclerView oncreateviewholder android 中的 NPE

转载 作者:行者123 更新时间:2023-12-01 07:49:13 26 4
gpt4 key购买 nike

这应该是一个一如既往的简单工作。我刚刚从 api 调用中获取数据,并希望将其填充到 recyclerview 中。我猜我已经像每个人一样这样做了数千次。但在这里,当 View 项布局在适配器类中膨胀时,我收到空指针异常。我不知道如何解决它。

我的适配器类是

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)

最佳答案

在你的layout.xml中更改<view<View

关于java - RecyclerView oncreateviewholder android 中的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41800976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com