有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java回收器视图线性布局管理器返回null

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

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

这是我目前的代码。我已经检查了我收到的数据是否完整

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_配方

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;
}

}

子适配器--RecipeIngRepientAdapter

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;
}

}

用于该功能的viewholder是一个包含图像的简单viewholder。 我在网上看到过一些帖子,它们似乎做了完全相同的事情,并让它发挥作用,我到底错过了什么


共 (1) 个答案

  1. # 1 楼答案

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