0

我得到這個錯誤嘗試空對象

顯示java.lang.NullPointerException上調用虛方法布爾RecyclerView $ ViewHolder shouldIgnore():嘗試調用虛擬方法「布爾android.support.v7.widget。 RecyclerView $ ViewHolder.shouldIgnore()'上的空對象引用

當我運行我的代碼。我看不出我犯了什麼錯誤。有誰知道爲什麼? 這是我的代碼

片段

public class NowPlayingFragment extends Fragment implements RecyclerItemClickListener.OnItemClickListener{ 
    ApiInterface mApiInterface; 
    private RecyclerView mRecyclerView; 
    private RecyclerView.Adapter mAdapter; 
    private RecyclerView.LayoutManager mLayoutManager; 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false); 
     mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.rootLayout); 
     mLayoutManager = new LinearLayoutManager(this.getActivity()); 
     mRecyclerView.setLayoutManager(mLayoutManager); 
     mRecyclerView.setHasFixedSize(true); 
     mApiInterface = ApiClient.getClient().create(ApiInterface.class); 
     mAdapter = new FilmAdapter(new ArrayList<Film>()); 
     mRecyclerView.setAdapter(mAdapter); 
     refresh(); 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.fragment_now_playing, container, false); 

     return v; 
    } 

    public void refresh() { 
     Call<List<Film>> call = mApiInterface.getFilmPlaying("2"); 
     call.enqueue(new Callback<List<Film>>() { 
      @Override 
      public void onResponse(Call<List<Film>> call, Response<List<Film>> response) { 
       List<Film> FilmList = response.body(); 
       Log.d("Retrofit Get", "Jumlah data : " + 
         String.valueOf(FilmList.size())); 
       mAdapter = new FilmAdapter(FilmList); 
       mRecyclerView.setAdapter(mAdapter); 
      } 

      @Override 
      public void onFailure(Call<List<Film>>call, Throwable t) { 
       Log.e("Retrofit Get", t.toString()); 
      } 
     }); 

     //mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), this)); 
    } 

    @Override 
    public void onItemClick(View childView, int position) { 

    } 

    @Override 
    public void onItemLongPress(View childView, int position) { 

    } 
} 

適配器

public class FilmAdapter extends RecyclerView.Adapter<FilmAdapter.FilmViewHolder> { 

    private List<Film> mFilmList; 
    private Context context; 

    public FilmAdapter(List <Film> FilmList) { 
      mFilmList= FilmList; 
    } 

    @Override 
    public FilmViewHolder onCreateViewHolder(ViewGroup parent,int viewType) { 
     View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_film, parent, false); 
     FilmViewHolder mViewHolder = new FilmViewHolder(mView); 
     return mViewHolder; 
    } 

    @Override 
    public void onBindViewHolder(FilmViewHolder holder, int position) { 
     Picasso.with(context).load("http://ecinema.esy.es/img/" + mFilmList.get(position).getBackdropPath()).resize(200, 250).into(holder.foto); 
    } 

    public static class FilmViewHolder extends RecyclerView.ViewHolder { 
     public ImageView foto; 

     public FilmViewHolder(View v) { 
      super(v); 
      foto = (ImageView) v.findViewById(R.id.posterFilm); 
     } 
    } 

    @Override 
    public int getItemCount() { 
     return mFilmList.size(); 
    } 
} 

預先感謝

+1

您是否閱讀過錯誤信息?你認爲這意味着什麼? – m0skit0

+0

@ m0skit0 nope,這是不同的 –

+0

檢查我的答案在這裏:https://stackoverflow.com/a/47436165/700693 –

回答

0

這似乎是由於在RecyclerView一個錯誤,這是在23.2.0引入。該錯誤報告爲here,我解釋了我認爲導致該錯誤的錯誤comment #5

這裏是我的解釋,這裏複製爲參考歷史的目的和易用性:

我發現這個問題的根源。在 RecyclerView.dispatchLayoutStep3()中,有一個for循環,「for(int i = 0; i < count; ++ i)」,其中計數基於 mChildHelper.getChildCount()。雖然這種迭代正在發生,所述 收集由ChildHelper管理由 ChildHelper.hideViewInternal(),這導致無效返回 從呼叫到mChildHelper.getChildAt()上 RecyclerView的3050線,這又導致在改性null在同一行代碼(RecyclerView:3050)上從 getChildViewHolderInt()返回。

這裏的方法的調用鏈的結果在修飾 中斷的for循環的完整性:

dispatchLayoutStep3() - )> animateChange( - > addAnimatingView() - > 隱藏() - > hideViewInternal()

當ChildHelper增加孩子參數去其mHiddenViews收集, 它違反的for循環的完整性 dispatchLayoutStep3一路()。

我看到這兩種解決方法:

1)在你的RecyclerView

2)降級到23.1.1,其中這不是一個問題

NullPointerException - Attempt to invoke virtual method RecyclerView$ViewHolder.shouldIgnore()' on a null object reference

禁止改變動畫
+0

我不使用動畫在我的RecyclerView和我使用25.0.0 –

+0

版本25.0.0也面臨這個問題。 – gahfy

+0

那我該怎麼辦? –