2016-12-15 6 views
2

여러 항목이 포함 된 리사이클 러 뷰가 있습니다. 리사이클 러 뷰의 각 항목에는 가로 리사이클 뷰가 포함되어 있습니다. 발생하는 문제는 레이아웃 관리자가 null이라는 것입니다.Recycler null을 반환하는 선형 레이아웃 관리자를 봅니다.

java.lang.NullPointerException이 : 널 객체 참조

에 '무효 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; 
} 

}

그 viewholder의 이미지를 포함하는 간단한 viewholder이다. 정확히 똑같은 일을하고 작동하도록하는 게시물을 온라인에서 보았습니다. 정확히 여기에없는 것이 있습니까?

+0

리사이클 러 뷰에 제출 한 잘못된 ID와 함께 어댑터의 getItemCount()를 변경하여 목록 크기를 반영하지 않으므로 항상 0이었고 어댑터에서 데이터를 전혀 표시하지 않았습니다. –

답변

3

오류로 인해 RecyclerView가 null이거나 LayoutManager가 아닌 것처럼 들립니다. 레이아웃에서 올바른 ID를 사용하고 있는지 확인하십시오. RecyclerView의 적절한 레이아웃 ID가 R.id.recyclerView입니까?

+0

나는 그걸 해결할 수 있었지만 자식 리사이클 뷰는 데이터가 있어도 아무 것도 보여주지 않기 때문에 나타나지 않는다. –

+0

setIngredients 메소드로 IngredientsAdapter에 데이터를 전달하지 마십시오. 또한 어댑터의 경우 일반적으로 생성자에 데이터를 전달하여 데이터를 초기화하려고합니다. 따라서 IngredientsAdapter의 생성자를 변경하여 2 개의 인수, 컨텍스트 및 데이터의 ArrayList를 가져옵니다. –