-2

Это должна быть простая работа, как всегда. Я просто получаю данные из своего вызова api и хочу заполнить его в recyclerview. Я делал это как тысячу раз, как я думаю. Но здесь я получаю исключение null-указателя, когда макет элемента представления раздувается в классе адаптера. Я понятия не имею, как его решить.NPE в recylerview oncreateviewholder android

Мой класс адаптер

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

private List<RecipeByIngredients> recipeByIngredients; 
private Context mContext; 

public RecipeAdapter(List<RecipeByIngredients> 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 является

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:autofit="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:minHeight="200dp"> 

<ImageView 
    android:id="@+id/ivRecipe" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:alpha="50" 
    android:scaleType="centerCrop" /> 

<me.grantland.widget.AutofitTextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical|center_horizontal" 
    android:padding="8dp" 
    android:text="Recipe name" 
    android:textAllCaps="false" 
    android:textColor="#FFFFFF" 
    android:textSize="@dimen/text_size_large" 
    autofit:minTextSize="10sp" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:orientation="horizontal" 
    android:weightSum="2"> 

    <me.grantland.widget.AutofitTextView 
     android:id="@+id/tvUsedIngredients" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:padding="8dp" 
     android:text="5 ingredients used" 
     android:textAllCaps="false" 
     android:textColor="#b7b7b7" 
     android:textSize="@dimen/text_size_small" 
     android:textStyle="bold|italic" 
     autofit:minTextSize="8sp" /> 

    <view 
     android:layout_width="0.5dp" 
     android:layout_height="20dp" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:background="#ffffff" /> 

    <me.grantland.widget.AutofitTextView 
     android:id="@+id/tvMissedIngredients" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:padding="8dp" 
     android:text="3 ingredients missed" 
     android:textAllCaps="false" 
     android:textColor="#b7b7b7" 
     android:textSize="@dimen/text_size_small" 
     android:textStyle="bold|italic" 
     autofit:minTextSize="8sp" /> 

</LinearLayout> 

</FrameLayout> 

Ошибка я получаю

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) 
+2

Журнал ошибок говорит, что вы вызываете 'String.equals (java.lang.Object)' на нуль ссылка на объект. Где вы называете метод 'equals()' в своем коде? Это должно быть причиной. – yrazlik

+0

Стекло показывает, что у вас есть как минимум 14 строк onCreateViewholder, и вы показываете только 3. Вы звоните в .equals() в любом месте в этих строках? – MidasLefko

+0

Нет У меня есть только 3 строки. проблема выясняется. Его мнение связано с проблемой расположения элементов. Спасибо за вашу заботу. Я принял правильный ответ, который вы можете проверить. –

ответ

5

В вашем изменении layout.xml <view в <View

+0

Человек, которого я только что спас. Большое спасибо. Даже я знаю причину, почему «вид» создает проблему, но я только что проверил ее сейчас. –

0

Попробовать это

изменить метод onCreateViewHolder() к этому

@Override 
    public RecipeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item_layout,parent,false); 
     ViewHolder rcv = new ViewHolder(layoutView,mContext); 
     return rcv; 



    } 
+0

Конструктор класса Viewholder не имеет параметра контекста, и я не думаю, что он создаст какую-либо разницу. –

Смежные вопросы