java.lang.NullPointerException at android.support.v7.widget.RecyclerView.onMeasure

后端 未结 5 517
梦毁少年i
梦毁少年i 2020-12-06 04:20

I try to use RecyclerView with RecyclerView.Adapter but here is something wrong. I post my code below:

Layout:

   

        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 05:18

    If your are facing the same issue for RecycleView inside a fragment, try to use this code.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_latest, container, false);
    
        // get recycler view
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.novel_item_list);
    
        //mRecyclerView.setHasFixedSize(true);
    
        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
    
    
        String[] abc = {"hi","how are you","this is recycler"};
        // specify an adapter (see also next example)
        mAdapter = new RecyclerViewAdapter(abc);
        mRecyclerView.setAdapter(mAdapter);
        return rootView;
    }
    

提交回复
热议问题