当前位置:  开发笔记 > 编程语言 > 正文

在recylerview中的NPE oncreateviewholder android

如何解决《在recylerview中的NPEoncreateviewholderandroid》经验,为你挑选了1个好方法。

这应该是一项简单的工作.我刚从api调用中获取数据,并希望在recyclerview中填充它.我做过这样的事情就像我猜的每个人一样.但是在这里,当视图项布局在适配器类中膨胀时,我得到空指针异常.我不知道如何解决它.

我的适配器类是

public class RecipeAdapter extends RecyclerView.Adapter {

private List recipeByIngredients;
private Context mContext;

public RecipeAdapter(List 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是










    

    

    




我得到的错误

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)

小智.. 6

在layout.xml中更改



1> 小智..:

在layout.xml中更改

推荐阅读
sx-March23
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有