首页 > 代码库 > RecyclerView的坑

RecyclerView的坑

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recycleView.setLayoutManager(layoutManager);
这一行报空指针
错误信息:
FATAL EXCEPTION: main
                                                                   Process: com.create.bicdroid, PID: 4997
                                                                   java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.support.v7.widget.RecyclerView.defaultOnMeasure(int, int)‘ on a null object reference
                                                                       at android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(RecyclerView.java:8144)
                                                                       at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2614)
                                                                       at android.view.View.measure(View.java:18948)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
                                                                       at com.zhy.autolayout.AutoLinearLayout.onMeasure(AutoLinearLayout.java:43)
                                                                       at android.view.View.measure(View.java:18948)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                       at android.view.View.measure(View.java:18948)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
                                                                       at android.view.View.measure(View.java:18948)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                       at com.zhy.autolayout.AutoFrameLayout.onMeasure(AutoFrameLayout.java:64)
                                                                       at android.view.View.measure(View.java:18948)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
                                                                       at com.zhy.autolayout.AutoLinearLayout.onMeasure(AutoLinearLayout.java:43)
                                                                       at android.view.View.measure(View.java:18948)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                       at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2713)
                                                                       at android.view.View.measure(View.java:18948)
                                                                       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2158)
                                                                       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1252)
                                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1488)
                                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
                                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6256)
                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:886)
                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:698)
                                                                       at android.view.Choreographer.doFrame(Choreographer.java:633)
                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:872)
                                                                       at android.os.Handler.handleCallback(Handler.java:743)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:150)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5665)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)

是在onmesure()方法中报错,网上解释的比较合理的原因是 这句代码必须是oncreate方法中调用,因为要计算空间尺寸,但是别的类也是同样的用法,就不会出现该错误。

 

RecyclerView的坑