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

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

I am getting following Exception

01-27 11:15:15.756  18348-18348/com.example.pnimje.newswipelistview E/AndroidRuntime: FATAL EXCEPTION: main     java.lang.NullPointerException             at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)             at android.view.View.measure(View.java:15848)             at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)             at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)             at android.view.View.measure(View.java:15848)             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)             at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)             at android.view.View.measure(View.java:15848)             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)             at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)             at android.view.View.measure(View.java:15848)             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)             at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)             at android.view.View.measure(View.java:15848)             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)             at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)             at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)             at android.view.View.measure(View.java:15848)             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)             at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)             at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)             at android.view.View.measure(View.java:15848)             at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)             at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)             at android.view.Choreographer.doCallbacks(Choreographer.java:562)             at android.view.Choreographer.doFrame(Choreographer.java:532)             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)             at android.os.Handler.handleCallback(Handler.java:730)             at android.os.Handler.dispatchMessage(Handler.java:92)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:5103)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:525)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)             at dalvik.system.NativeStart.main(Native Method) 

and RecyclerView.java:1764 is

    mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec); 

Why is mLayout (LayoutManager) null?

回答1:

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()); 


回答2:

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)); 


回答3:

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);   } 


回答4:

You need to verify that what you are sending to the adapter is not an empty List. 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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!