[英]Android app crashed when change orientation
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)
这是错误指向** 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);
Your exception is saying that you are trying to use setLayoutManager
on null object reference
.您的例外是说您正在尝试在null object reference
上使用setLayoutManager
。 This means that your recylcerView
is null at the moment.这意味着您的 recyclerView 目前是recylcerView
。 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
.因此, onCreate
将再次被调用,其他一切都像你刚刚开始新的Activity
。 Sometimes issues happen here so there is few options on how to handle these events.有时这里会发生问题,因此关于如何处理这些事件的选择很少。
Retaining an object during a configuration change在配置更改期间保留 object
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.这可以通过 Bundle 完成,但如果您有大量数据,那么最好使用 ViewModel object。 How to do this check here:如何在此处进行此检查:
Handling the configuration change yourself自己处理配置更改
This other option comes with big CAUTION .这个其他选项带有很大的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.如果您无法保存您的实例,那么我建议您这样做,但首先,尝试使用 ViewModel 和 Bundles 来解决此问题。
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.将android:configChanges
行添加到您的<activity/>
标签内的AndroidManifest.xml
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.