Cause for NullPointerException android.support.v7.widget.RecyclerView.onMeasure

后端 未结 4 1179
北海茫月
北海茫月 2020-12-15 17:44

I am getting following Exception

01-27 11:15:15.756  18348-18348/com.example.pnimje.newswipelistview E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.Nu         


        
相关标签:
4条回答
  • 2020-12-15 17:47

    because you have not set LinearLayoutManager to RecyclerView.

    for example:

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(new CustomAdapter());
    
    0 讨论(0)
  • 2020-12-15 17:53

    You need to verify that what you are sending to the adapter is not an empty List<Type>. I recommend you to either create the RecyclerView by code and add it later when you have the information, or to put an empty NULL object before passing it to the constructor of the adapter.

    0 讨论(0)
  • 2020-12-15 17:58

    Antón response is good but can be confusing in beginners like me, talves this serves to clarify some doubts.

    RecyclerView rv;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buscar);
    
        rv = (RecyclerView) findViewById(R.id.RecViewTest);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        rv.setLayoutManager(llm);
    
    
    }
    
    0 讨论(0)
  • 2020-12-15 18:10

    These kinds of errors usually originate in not setting the RecyclerView's layout manager. Bein' in a hurry myself I forgot to set one which ended in the following error:

    java.lang.NullPointerException
            at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:1636)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1820)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2177)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1878)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2177)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1878)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2177)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1878)
    

    The following line should resolve it:

    mRecyclerView.setLayoutManager(new LinearLayoutManager((Activity)this));
    
    0 讨论(0)
提交回复
热议问题