我得到這個錯誤嘗試空對象
顯示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();
}
}
預先感謝
您是否閱讀過錯誤信息?你認爲這意味着什麼? – m0skit0
@ m0skit0 nope,這是不同的 –
檢查我的答案在這裏:https://stackoverflow.com/a/47436165/700693 –