将图像加载到RecyclerView中会显示“未附加适配器;跳过布局”错误

时间:2015-07-19 06:10:52

标签: android android-recyclerview android-adapter rx-java

我收到一条错误,说“没有连接适配器;跳过布局”,但是我的图片正好加载到我的LayoutManager中。但是,当我尝试向下滚动并加载其他数据时,应用程序崩溃并出现NullPointerException。这是我的相关代码。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    service.getPodcasts()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(podcasts -> mRecyclerView.setAdapter(new PodcastsAdapter(getActivity(), podcasts)));
}

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_podcasts, container, false);

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.podcasts_recycler_view);
    mLayoutManager = new GridLayoutManager(getActivity(), 3);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    //mRecyclerView.setAdapter(mAdapter);

    return rootView;
}

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:-1)

您没有附加适配器,因为您在尝试附加适配器后创建它​​:

就我而言:

mRecyclerView.setAdapter(mAdapter); // Here, mAdapter is null
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());