本文介绍了应用轰然试图用RecyclerView在Android 5.0时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想乱用新RecyclerView,每当我尝试运行它,我的应用程序立即崩溃。它给我的NullPointerException从 android.support.v7.widget.RecyclerView 试图访问方法。我看着其他职位,看到大多数人没有编译com.android.support:recyclerview-v7:+,但我想,它不是招'T帮助都没有。真的不知道该怎么做,在这一点上,任何帮助,将AP preciated。在这里,错误日志:(我会发布图片,但我没有10代表还)

 显示java.lang.NullPointerException:尝试调用虚拟方法无效android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView $州,在一个空对象引用INT,INT)
        在android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
        在android.view.View.measure(View.java:17430)
        在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:727)
        在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
        在android.view.View.measure(View.java:17430)
        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        在android.view.View.measure(View.java:17430)
        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        在android.view.View.measure(View.java:17430)
        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        在android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        在android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        在android.view.View.measure(View.java:17430)
        在android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
        在android.view.View.measure(View.java:17430)
        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        在android.view.View.measure(View.java:17430)
        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        在android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        在android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        在android.view.View.measure(View.java:17430)
        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        在android.view.View.measure(View.java:17430)
        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        在android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        在android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        在android.view.View.measure(View.java:17430)
        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2560)
        在android.view.View.measure(View.java:17430)
        在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
        在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
        在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
        在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
        在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5786)
        在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)
        在android.view.Choreographer.doCallbacks(Choreographer.java:580)
        在android.view.Choreographer.doFrame(Choreographer.java:550)
        在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)
        在android.os.Handler.handleCallback(Handler.java:739)
        在android.os.Handler.dispatchMessage(Handler.java:95)
        在android.os.Looper.loop(Looper.java:135)
        在android.app.ActivityThread.main(ActivityThread.java:5221)
        在java.lang.reflect.Method.invoke(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:372)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:898)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
 

解决方案

这个问题通常发生在没有<$c$c>LayoutManager商为<$c$c>RecyclerView.你可以做到这一点,像这样:

 最后LinearLayoutManager的layoutManager =新LinearLayoutManager(上下文);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(的layoutManager);
 

I'm trying to mess with the new RecyclerView and whenever I try to run it, my app immediately crashes. It gives me NullPointerException for trying to access methods from android.support.v7.widget.RecyclerView. I've looked at other posts and saw that most people didn't have compile 'com.android.support:recyclerview-v7:+' but I tried that and it hasn't helped at all. Not really sure what to do at this point, any help would be appreciated. Here the error log: (I would post a picture but I don't have 10 rep yet)

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
        at android.view.View.measure(View.java:17430)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17430)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
        at android.view.View.measure(View.java:17430)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5786)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
解决方案

This issue usually occurs when no LayoutManager was provided for the RecyclerView. You can do it like so:

final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);

这篇关于应用轰然试图用RecyclerView在Android 5.0时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:21