应用程序崩溃使用RecyclerView

时间:2015-02-23 12:18:20

标签: android android-recyclerview

我在我的应用中使用RecyclerView。我的应用程序因以下错误而崩溃

  

java.lang.NullPointerException:尝试调用虚方法' void android.support.v7.widget.RecyclerView $ LayoutManager.onMeasure(android.support.v7.widget.RecyclerView $ Recycler,android.support.v7 .widget.RecyclerView $ State,int,int)'在空对象引用上

在stackoverflow上搜索时,我发现这个错误通常在没有添加layoutmanager时出现。但我添加了布局管理器。请帮我找一下造成这次事故的原因。

编辑 - 使用Recyclerview的片段代码

public class FeedFragment extends Fragment implements HJAppConstants {

    private RecyclerView mRecyclerView;

    private FeedRecyclerAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.hj_feed_layout, null);
        mRecyclerView = (RecyclerView) view
                .findViewById(R.id.feeds_recycler_view);
        return view;
    }

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

        LinearLayoutManager layoutManager = new LinearLayoutManager(
                getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new FeedRecyclerAdapter();
        mRecyclerView.setAdapter(mAdapter);
        addData();

    }

    /**
     * Adds static data
     */
    private void addData() {
        ArrayList<HJFeedItemObject> itemObjs = new ArrayList<HJFeedItemObject>();
        itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_PHOTO,
                "potala_palace.jpg", "This is an article about Potala Palace",
                "Thu 7, 07:45pm", new HJUserObject("Umesh", "")));
        itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_PRODUCT, "pokhara.jpg",
                "A beautiful place to visit - Pokhara ", "Thu 7, 07:45pm",
                new HJUserObject("Umesh", "")));
        itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_ARTICLE, "patan.jpg",
                "A Tourist attraction - Patan", "Thu 7, 07:45pm",
                new HJUserObject("Umesh", "")));
        itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_PHOTO,
                "potala_palace.jpg", "This is an article about Potala Palace",
                "Thu 7, 07:45pm", new HJUserObject("Umesh", "")));
        itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_PRODUCT, "pokhara.jpg",
                "A beautiful place to visit - Pokhara ", "Thu 7, 07:45pm",
                new HJUserObject("Umesh", "")));
        itemObjs.add(new HJFeedItemObject(HJ_FEED_TYPE_ARTICLE, "patan.jpg",
                "A Tourist attraction - Patan", "Thu 7, 07:45pm",
                new HJUserObject("Umesh", "")));
        mAdapter.addData(itemObjs);
    }

1 个答案:

答案 0 :(得分:0)

如果没有为RecyclerView提供LayoutManager,则会出现此问题。

更改您的代码,并将layoutManager设置移动到onCreateView。

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

   View view = inflater.inflate(R.layout.hj_feed_layout, null);
   mRecyclerView = (RecyclerView) view
                .findViewById(R.id.feeds_recycler_view);

   LinearLayoutManager layoutManager = new LinearLayoutManager(
                getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);

   return view;
}