Null pointer exception for RecyclerView.onMeasure (...)

I am trying to fix exception

that is preventing my android app from starting. I searched for this exception and found others with the same problem. Some have fixed the problem by including LinearLayoutManager

for their own RecycledView

, others have included the code for that manager in the method onCreate()

, and others have included compilation 'com.android.support:recyclerview-v7:21.0.0'

in their file build.gradle

.

I've tried all three and my problem still remains. I would like to know why.

Here is my code from 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 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.

+3


source to share


3 answers


Looks like I included an additional one RecyclerView

in my XML

file outside the tag LinearLayout

. By simply removing this extra RecyclerView

, the application will work without exception

.



0


source


Try it,

  mRecyclerView .setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));

      



It looks like the issue is context related

0


source


add

// gets recyclerView
   mRecyclerView = (RecyclerView) findViewById(R.id.list);
// manager for recyclerView
   recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));

      

0


source







All Articles