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

Recycler View linear layout manager returning null

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

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

这是我的代码,所以 far.I 已检查我收到的数据是否完好无损。

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 吗?