-2

我綁定視圖時收到此錯誤。
當我不添加任何標題時,代碼工作正常。
視圖正在正確初始化並且數據正在顯示。
但是,每當我嘗試添加標題,我得到這個異常。
我已經初始化所有視圖,但它不能只是獲得參考。
而突出顯示的可能的問題並不能解決問題,因爲我在傳遞參考後綁定視圖。
所以我不通過引用之前調用視圖。NullPointerException異常時添加標頭RecyclerView

FATAL EXCEPTION: main 
Process: com.example.shashankdc.learn, PID: 8615 
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
    at com.example.shashankdc.learn.Medals.Medals_Adapter.onBindViewHolder(Medals_Adapter.java:62) 
    at com.example.shashankdc.learn.Medals.Medals_Adapter.onBindViewHolder(Medals_Adapter.java:17) 
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5138) 
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4433) 
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326) 
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955) 
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1364) 
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327) 
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:556) 
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2713) 
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3011) 
    at android.view.View.layout(View.java:15695) 
    at android.view.ViewGroup.layout(ViewGroup.java:4981) 
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076) 
    at android.view.View.layout(View.java:15695) 
    at android.view.ViewGroup.layout(ViewGroup.java:4981) 
    at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1009) 
    at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:719) 
    at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) 
    at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:984) 
    at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:732) 
    at android.view.View.layout(View.java:15695) 
    at android.view.ViewGroup.layout(ViewGroup.java:4981) 
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 
    at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 
    at android.view.View.layout(View.java:15695) 
    at android.view.ViewGroup.layout(ViewGroup.java:4981) 
    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:15695) 
    at android.view.ViewGroup.layout(ViewGroup.java:4981) 
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 
    at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 
    at android.view.View.layout(View.java:15695) 
    at android.view.ViewGroup.layout(ViewGroup.java:4981) 
    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:15695) 
    at android.view.ViewGroup.layout(ViewGroup.java:4981) 
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 
    at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 
    at android.view.View.layout(View.java:15695) 
    at android.view.ViewGroup.layout(ViewGroup.java:4981) 
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2186) 
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1920) 
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106) 
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:596) 
    at android.view.Choreographer.doFrame(Choreographer.java:557) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778) 
    at android.os.Handler.handleCallback(Handler.java:739) 

這是我的適配器

package com.example.shashankdc.learn.Medals; 

import android.content.Context; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import com.example.shashankdc.learn.R; 

import java.util.List; 


public class Medals_Adapter extends RecyclerView.Adapter<Medals_Adapter.MyHolder> { 

    private static final int TYPE_HEAD = 0; 
    private static final int TYPE_LIST = 1; 
    private List<Medals_Model> mylist; 
    private Context context; 

    public Medals_Adapter(List<Medals_Model> mylist, Context context) { 
     this.mylist = mylist; 
     this.context = context; 
    } 

    @Override 
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     if (viewType == TYPE_LIST) { 

      View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.medals_card, parent, false); 
      return new MyHolder(v, viewType); 
     } else if (viewType == TYPE_HEAD) { 
      View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_img, parent, false); 
      return new MyHolder(v, viewType); 
     } 

     return null; 
    } 

    @Override 
    public void onBindViewHolder(MyHolder holder, int position) { 


     if (holder.view_type == TYPE_LIST) { 

      Medals_Model model = mylist.get(position + 1); 
      holder.name.setText(model.getName()); 
      holder.requirements.setText(model.getRequirements()); 
      holder.bronze.setText(String.valueOf(model.getBronze())); 
      holder.silver.setText(String.valueOf(model.getSilver())); 
      holder.gold.setText(String.valueOf(model.getGold())); 

     } else if (holder.view_type == TYPE_HEAD) { 

      holder.header.setText("HEADER"); 

     } 


    } 

    @Override 
    public int getItemCount() { 
     return mylist.size() + 1; 
    } 

    public void add(Medals_Model model) { 
     mylist.add(model); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public int getItemViewType(int position) { 
     if (position == 0) { 
      return TYPE_HEAD; 
     } else { 
      return TYPE_LIST; 
     } 
    } 

    public class MyHolder extends RecyclerView.ViewHolder { 
     public TextView name, requirements, gold, silver, bronze; 
     //Header 
     public TextView header; 
     int view_type; 

     public MyHolder(View itemView, int view_type) { 
      super(itemView); 

      if (view_type == TYPE_LIST) { 
       name = (TextView) itemView.findViewById(R.id.medals_title); 
       requirements = (TextView) itemView.findViewById(R.id.medals_requirements); 
       gold = (TextView) itemView.findViewById(R.id.medals_gold); 
       silver = (TextView) itemView.findViewById(R.id.medals_silver); 
       bronze = (TextView) itemView.findViewById(R.id.medals_bronze); 
       view_type = 1; 
      } else if (view_type == TYPE_HEAD) { 
//Header_config 
       header = (TextView) itemView.findViewById(R.id.my_header); 
       view_type = 0; 
      } 


     } 
    } 

} 

這裏是我的header_img.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="40dp"> 

    <android.support.v7.widget.CardView 
    android:layout_width = "match_parent" 
    android:layout_height = "wrap_content" 
    card_view:cardCornerRadius="3dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:id="@+id/my_header" 
     android:layout_height="wrap_content" /> 

</android.support.v7.widget.CardView> 
</LinearLayout> 

而且content_medals(獎章).XML

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.shashankdc.learn.Medals.Medals" 
    tools:showIn="@layout/medals"> 
    <android.support.v7.widget.RecyclerView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/recyclerView_medals" 
    android:background="@drawable/linearlayour_gradient" 
    /> 
    </RelativeLayout> 
+0

在這個特殊的解決方案中,調用是在沒有首先引用視圖的情況下進行的。在這裏,我在創建OnCreateViewHolder方法時傳遞引用。之後,我引用了OnBindViewHolder()中的視圖。 – shady

+0

'Medals_Adapter.java'第62行...該TextView爲空。請調試並找出原因 –

+0

如果您需要幫助,則應提供'header_img.xml'和'medals_card.xml' –

回答

0

這些生產線正在改變的參數呃,這是shadowing該字段。

view_type = 1; 
view_type = 0; 

請在ViewHolder構造更新要符合條件,要設置

public MyHolder(View itemView, int view_type) { 
    super(itemView); 

    this.view_type = view_type; // Do it here 
    if (view_type == TYPE_LIST) { 
     // Remove view_type = 1; 
    } else if (...) 
     // Remove view_type = 0; 

你的錯誤是下降通過類似於這種情況下一些東西,因爲holder.view_type總是0

if (holder.view_type == TYPE_LIST) { 

    Medals_Model model = mylist.get(position + 1); 

    // Error because holder.name is not defined for a header 
    holder.name.setText(model.getName()); 
正確的變量

或者,使用getItemViewType(position)而不是依靠holder.view_type

+0

非常感謝。使用this.view_type = view_type更新構造函數解決了這個問題。由於view_type在構造函數被調用時總是爲零。 – shady