-2

它應該是一個簡單的工作一如既往。我一直只是從我的api調用中獲取數據,並希望將其填充到recyclerview中。我猜測每個人都這樣做了好幾千次。但是在這裏,當視圖項目佈局在適配器類中膨脹時,我得到空指針異常。我不知道如何解決它。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

stacktrace表明你至少有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類的構造函數沒有上下文參數,我不認爲它會產生任何區別。 –