-3

When the orientation of app change this error is occur kindly help me

W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.explore_kpk, PID: 24892
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.explore_kpk/com.example.explore_kpk.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5389)
        at android.app.ActivityThread.access$1200(ActivityThread.java:223)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7231)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     ***Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference***
        ***at com.example.explore_kpk.MainActivity.onCreate(MainActivity.java:121)
        at android.app.Activity.performCreate(Activity.java:6877)***
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352) 
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5389) 
        at android.app.ActivityThread.access$1200(ActivityThread.java:223) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7231) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
I/Process: Sending signal. PID: 24892 SIG: 9

This is code in which error pointing** at com.example.explore_kpk.MainActivity.onCreate(MainActivity.java:121)

  RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recyler_view);

        RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this, recyclerList);
        long seed = System.nanoTime();
        Collections.shuffle(recyclerList, new Random(seed));

        GridLayoutManager gridLayoutManager=new GridLayoutManager(this, mNoOfColumns);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(recyclerViewAdapter);
Raghunandan
  • 129,147
  • 24
  • 216
  • 249

1 Answers1

1

Your exception is saying that you are trying to use setLayoutManager on null object reference. This means that your recylcerView is null at the moment. I don't see your whole code so I can't give you an instant solution. What I can say for you is that each time the screen orientation is changed your current activity will be destroyed and called again. So, onCreate will be called again and everything else like you just started new Activity. Sometimes issues happen here so there is few options on how to handle these events.


Retaining an object during a configuration change

What this means is for you to keep your data while screen rotation is happening. This can be done with Bundle but if you have a large set of data then it is better to use the ViewModel object. How to do this check here:


Handling the configuration change yourself

This other option comes with big CAUTION. Consider this as a last resort when you really need to avoid restarts because of a configuration change. What this does is preventing your activity from the restart. If you can't save your instance then I would recommend doing this but first, try to play with ViewModel and Bundles to fix this issue.

How to do this?

<activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize|screenLayout" />

add android:configChanges line to your AndroidManifest.xml inside your <activity/> tag.

SlothCoding
  • 1,255
  • 2
  • 11