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

后端 未结 5 500
梦毁少年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 04:56

    I got this error when my RecyclerView had no LayoutManager. Adding this code fixed the problem:

    recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
    
    0 讨论(0)
  • 2020-12-06 05:02

    Your xml shows that you have two android namespaces which actually should give you an error because in android you are allowed to use the namespace only once. Remove the linearLayout from your main layout as it seems unnecessary. Check your custom Row layout i.e topic_tile as well for any similar errors

    <android.support.v7.widget.RecyclerView 
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/topic_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    EDIT :

    in onCreate()

    private RecyclerView.LayoutManager mLayoutManager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            .
            . 
            .
            mLayoutManager = new LinearLayoutManager(this);
            recycleView.setLayoutManager(mLayoutManager);
        }
    
    0 讨论(0)
  • 2020-12-06 05:06

    For anyone trying to use a ViewPager for tabs, and inside their tabs have a RecyclerView which causes this crazy error, I suggest to postpone inflating by creating yet another Fragment for including only the RecylerView.

    This xml file is one of my tabs which contain a frame layout instead a recycler .

    shop_most_views_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <FrameLayout
        android:id="@+id/most_view_item_frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f3f3f3" />
    </LinearLayout>
    

    its java class

    public class MostViews extends Fragment {
    private String title;
    private int page;
    View convertedView;
    RecyclerView grids;
    
    public static MostViews newInstance(int page, String title) {
        MostViews fragmentFirst = new MostViews();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page = getArguments().getInt("someInt", 0);
        title = getArguments().getString("someTitle");
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        convertedView = inflater.inflate(R.layout.shop_most_views_fragment, container, false);
        transitMasonaryLayoutFragmentIntoFramLayout();
        return convertedView;
    }
    
    private void transitMasonaryLayoutFragmentIntoFramLayout() {
        MasanoryLayout masanoryLayout = new MasanoryLayout();
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .add(R.id.most_view_item_frameLayout, masanoryLayout).commit();
    }
    }
    

    so we have a frame layout to put our recylcer fragment into it.

    shop_most_view_masanory_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mostViewsItem"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </LinearLayout>
    

    its java class

    public class MasanoryLayout extends Fragment {
    View convertedView;
    RecyclerView grids;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        convertedView = inflater.inflate(R.layout.shop_most_view_masanory_fragment, container, false);
        grids = (RecyclerView) convertedView.findViewById(R.id.mostViewsItem);
        setLayoutManagerOnRecyclerView();
        MasonryAdapter adapter = new MasonryAdapter(initializeData());
        grids.setAdapter(adapter);
        return convertedView;
    }
    
    private void setLayoutManagerOnRecyclerView() {
        grids.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
    }
    
    private List<StoreItem> initializeData() {
        List<StoreItem> storeItems = new ArrayList<>();
        storeItems.add(new StoreItem("احسان", "احسان", R.drawable.detailed_index_store));
        storeItems.add(new StoreItem("احسان", "احسان", R.drawable.clothstore));
        storeItems.add(new StoreItem("احسان", "احسان", R.drawable.detailed_index_store));
        storeItems.add(new StoreItem("احسان", "احسان", R.drawable.clothstore));
        storeItems.add(new StoreItem("احسان", "احسان", R.drawable.detailed_index_store));
        storeItems.add(new StoreItem("احسان", "احسان", R.drawable.detailed_index_store));
        storeItems.add(new StoreItem("احسان", "احسان", R.drawable.clothstore));
    
        return storeItems;
    }
    }
    

    as you see your FragmentPagerAdapter would call MostViews to be inflated and MostViews itself would call MasanoryLayout to be inflated. so we escaped FragmentPagerAdapter context ;)

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-06 05:18

    You have to set LayoutManager for RecycleView:

    Currently, android supports 3 kinds of layouts.

    If you want it seems a normal ListView, use LinearLayoutManager.

    If you want it seems a gridView, use GridLayoutManager.

    If you want it seem staggered, use StaggeredGridLayoutManager

    Constructors for 3 kinds layouts above

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
    
    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.HORIZONTAL);
    

    Then you set LinearLayout to RecycleView ( in this case I used LinearLayoutManager

    recyclerView = (RecyclerView) view.findViewById(R.id.saved_recycle_view);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
    recyclerView.setLayoutManager(linearLayoutManager);
    

    Anyway, use OttoBus Library to pass out events from the Adapter to Activity/Fragment containing the RecycleView. Good luck !

    0 讨论(0)
提交回复
热议问题