[英]Null Pointer Exception for RecyclerView.onMeasure(…)
I am trying to fix an exception
that stops my Android app from running. 我正在尝试修复阻止我的Android应用运行的exception
。 I have googled this exception and found others with the same problem. 我搜索了这个例外,并发现其他人有同样的问题。 Some have fixed the problem by including a LinearLayoutManager
for their RecycledView
, others have included the code for this manager in the onCreate()
method, and others have included compile 'com.android.support:recyclerview-v7:21.0.0'
into their build.gradle
file. 有些人已经通过为他们的RecycledView
包含一个LinearLayoutManager
解决了这个问题,其他人已经在onCreate()
方法中包含了这个管理器的代码,还有一些包括编译'com.android.support:recyclerview-v7:21.0.0'
到他们的build.gradle
文件。
I have tried all three, and my problem still remains. 我已经尝试了所有三个,我的问题仍然存在。 I'd like to know why. 我想知道为什么。
Here is my code from onCreate():
这是我在onCreate():
代码onCreate():
// gets recyclerView
mRecyclerView = (RecyclerView) findViewById(R.id.list);
// manager for recyclerView
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// animations for recyclerViews
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mAdapter = new GameAdapter(GameManager.getInstance().getGamesFromParse(), R.layout.card_view, this);
mRecyclerView.setAdapter(mAdapter);
What do you think is wrong? 你觉得怎么了?
Here is the exception
stack: 这是exception
堆栈:
05-27 11:56:50.386 7756-7756/games.picup.com.picup E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: games.picup.com.picup, PID: 7756
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1694)
at android.view.View.measure(View.java:17619)
at android.support.v4.widget.SwipeRefreshLayout.onMeasure(SwipeRefreshLayout.java:559)
at android.view.View.measure(View.java:17619)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:17619)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17619)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17619)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2588)
at android.view.View.measure(View.java:17619)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2317)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1412)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1613)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1270)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6691)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
at android.view.Choreographer.doCallbacks(Choreographer.java:613)
at android.view.Choreographer.doFrame(Choreographer.java:583)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5731)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Edit: Solved, see my answer. 编辑:已解决,请参阅我的回答。
Try this, 尝试这个,
mRecyclerView .setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));
Looks like problem is with the context 看起来问题与上下文有关
add 加
// gets recyclerView
mRecyclerView = (RecyclerView) findViewById(R.id.list);
// manager for recyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
It looks like I included an extra RecyclerView
in my XML
file outside of the LinearLayout
tag. 看起来我在LinearLayout
标记之外的XML
文件中包含了额外的RecyclerView
。 By simply removing this extra RecyclerView
, the app runs without an exception
. 通过简单地删除这个额外的RecyclerView
,该应用程序无exception
运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.