我正在尝试修复阻止我的Android应用运行的exception
。我搜索了这个例外,并发现其他人有同样的问题。有些人通过为LinearLayoutManager
添加RecycledView
来修复此问题,其他人已在onCreate()
方法中包含此管理员的代码,其他人已将编译'com.android.support:recyclerview-v7:21.0.0'
纳入其中build.gradle
档案。
我已经尝试了所有三个,我的问题仍然存在。我想知道为什么。
以下是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);
你认为错在哪里?
这是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)
编辑:已解决,请参阅我的回答。
答案 0 :(得分:0)
试试这个,
mRecyclerView .setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));
看起来问题与上下文有关
答案 1 :(得分:0)
添加
// gets recyclerView
mRecyclerView = (RecyclerView) findViewById(R.id.list);
// manager for recyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
答案 2 :(得分:0)
看起来我在RecyclerView
标记之外的XML
文件中添加了额外的LinearLayout
。只需删除此额外RecyclerView
,该应用即可在没有exception
的情况下运行。