繁体   English   中英

Recycler View线性布局管理器返回null

[英]Recycler View linear layout manager returning null

我有一个包含多个项目的回收站视图,并且回收站视图中的每个项目都包含一个水平回收站视图。我遇到的问题是布局管理器为null。

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $ LayoutManager)'

到目前为止,这是我的代码。我检查了我收到的数据是否完整。

RecipeAdapter(主适配器)

public class RecipeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private List<Object> items;

private final int RECIPE = 0, JOKE = 1;

public RecipeAdapter(Context context) {
    this.context = context;
    this.items = new ArrayList<>();
}

public void setItems(List<Object> items) {
    this.items = items;
}

public void add(Object object) {
    items.add(object);
    notifyItemInserted(items.size());
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder;
    final LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    switch (viewType) {
        case RECIPE:
            View recipe = inflater.inflate(R.layout.item_recipe, parent, false);
            viewHolder = new ViewHolder_Recipe(recipe);
            break;
        case JOKE:
            View joke = inflater.inflate(R.layout.item_joke, parent, false);
            viewHolder = new ViewHolder_Joke(joke);
            break;
        default:
            View recipe_default = inflater.inflate(R.layout.item_recipe, parent, false);
            viewHolder = new ViewHolder_Recipe(recipe_default);
            break;
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case RECIPE:
            ViewHolder_Recipe viewHolderRecipe = (ViewHolder_Recipe) holder;
            configureRecipeHolder(viewHolderRecipe, position);
            break;
        case JOKE:
            ViewHolder_Joke viewHolderJoke = (ViewHolder_Joke) holder;
            configureJokeHolder(viewHolderJoke, position);
            break;
        default:
            ViewHolder_Recipe viewHolder_recipe_default = (ViewHolder_Recipe) holder;
            configureRecipeHolder(viewHolder_recipe_default, position);
            break;
    }
}

private void configureJokeHolder(ViewHolder_Joke viewHolderJoke, int position) {

}

private void configureRecipeHolder(ViewHolder_Recipe viewHolderRecipe, int position) {

    RecipeDetailed recipe = (RecipeDetailed) items.get(position);

    Glide.with(context)
            .load(recipe.getImage())
            .into(viewHolderRecipe.getRecipe_image());

    viewHolderRecipe.getRecipe_name().setText(recipe.getTitle());
    viewHolderRecipe.getRecipe_prep().setText(recipe.getReadyInMinutes());
    viewHolderRecipe.getRecipe_serves().setText(recipe.getServings());

    viewHolderRecipe.getIngredientAdapter().setIngredients(recipe.getExtendedIngredients());
}

@Override
public int getItemViewType(int position) {
    if (items.get(position) instanceof RecipeDetailed) {
        return RECIPE;
    } else if (items.get(position) instanceof Joke) {
        return JOKE;
    }

    return super.getItemViewType(position);
}

@Override
public int getItemCount() {
    return items.size();
}

}

该适配器的ViewHolder-ViewHolder_Recipe

public class ViewHolder_Recipe extends RecyclerView.ViewHolder {

private CircularImageView recipe_image;
private TextView recipe_name;
private TextView recipe_prep;
private TextView recipe_serves;
private RecyclerView recyclerView;
private RecipeIngredientAdapter ingredientAdapter;

public ViewHolder_Recipe(View itemView) {
    super(itemView);
    recipe_image = (CircularImageView) itemView.findViewById(R.id.recipe_image);
    recipe_name = (TextView) itemView.findViewById(R.id.recipe_name);
    recipe_prep = (TextView) itemView.findViewById(R.id.recipe_prep);
    recipe_serves = (TextView) itemView.findViewById(R.id.recipe_serves);
    recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);

    ingredientAdapter = new RecipeIngredientAdapter(itemView.getContext());
    recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()
            , LinearLayoutManager.HORIZONTAL, false));
    recyclerView.setAdapter(ingredientAdapter);
}

public RecipeIngredientAdapter getIngredientAdapter() {
    return ingredientAdapter;
}

public CircularImageView getRecipe_image() {
    return recipe_image;
}

public TextView getRecipe_name() {
    return recipe_name;
}

public TextView getRecipe_prep() {
    return recipe_prep;
}

public TextView getRecipe_serves() {
    return recipe_serves;
}

public RecyclerView getRecyclerView() {
    return recyclerView;
}

}

子适配器-RecipeIngredientAdapter

public class RecipeIngredientAdapter extends RecyclerView.Adapter<ViewHolderRecipeIngredient> {

private Context context;
private ArrayList<Ingredient> ingredients;

public RecipeIngredientAdapter(Context context) {
    this.context = context;
}

public void setIngredients(ArrayList<Ingredient> ingredients) {
    this.ingredients = ingredients;
}

@Override
public ViewHolderRecipeIngredient onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.item_recipe_ingredients, parent, false);
    return new ViewHolderRecipeIngredient(view);
}

@Override
public void onBindViewHolder(ViewHolderRecipeIngredient holder, int position) {

    final Ingredient ingredient = ingredients.get(position);
    Glide.with(context)
            .load(ingredient.getImage())
            .into(holder.getIngredient_image());

}

@Override
public int getItemCount() {
    return 0;
}

}

该视图的持有人是一个简单的包含图像的视图持有人。 我在网上看到过一些帖子,它们似乎在做同样的事情并使它正常工作,我在这里到底缺少什么?

从错误中,听起来RecyclerView为null,而不是LayoutManager。 确保您使用的是布局中正确的ID。 您确定RecyclerView的正确布局ID是R.id.recyclerView吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM