简体   繁体   English

Android 应用程序在更改方向时崩溃

[英]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中。

当 object 添加到 ArrayList 时,Android 应用程序崩溃<object><div id="text_translate"><p>我是新手,因此对任何错误表示歉意。</p><p> 背景:作为 object 培训的一部分,我正在尝试添加 object 练习的 arraylist。 然而。 每次我尝试将 object 练习添加到训练的 Arraylist 时,应用程序都会崩溃。 欢迎任何帮助。</p><p> 对象训练和锻炼看起来像这样</p><pre>public class Training { public Integer trainingNumber; public Date datum; public Integer nrOfExercises; public ArrayList&lt;Exercise&gt; exercises; } public class Training { public Integer trainingNumber; public Date datum; public Integer nrOfExercises; public ArrayList&lt;Exercise&gt; exercises; }</pre><p> 试图生成所有这些的代码如下:</p><pre> private Training createRandomTraining(int i){ Training randomTraining = new Training(); Random random = new Random(); int d = random.nextInt(30)+1; int m = random.nextInt(12)+1; //int y = random.nextInt(21)+2000; int amountOfExercises = random.nextInt(9-6)+6; randomTraining.trainingNumber = i; randomTraining.datum = new Date(2021,m,d); for (int j = 0; j &lt; amountOfExercises; j ++){ Exercise exercise = new Exercise(); Random r = new Random(); int set = r.nextInt(5)+1; int nameInt = r.nextInt(3); int rep = r.nextInt(12)+1; //int y = random.nextInt(21)+2000; exercise.trainingNumber = i; exercise.exerciseNumber = j+1; exercise.exerciseName = getExerciseName(nameInt); exercise.sets = set; exercise.reps = rep; exercise.weight = random.nextInt(200-50)+50; randomTraining.exercises.add(exercise); //the app crashes here. //The other parts work fine. } randomTraining.nrOfExercises = amountOfExercises; return randomTraining; }</pre></div></object> - Android app crashed when object is added to ArrayList<Object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试打开它时,Android 应用程序崩溃了 - Android App crashed when i try to open it Android方向更改导致应用程序崩溃 - Android Orientation change results in app crashing Android应用程序在setContentView上崩溃 - Android app Crashed on setContentView 使用wakelock.release()关闭应用程序时Android应用程序崩溃 - Android app crashed when closing app using wakelock.release() 方向发生变化时,android中的片段无法正常工作 - fragment in android not working when change in orientation happens 更改网页视图大小时,我的应用程序崩溃了 - my app crashed when I change webview size 按下主屏幕按钮但方向更改正常时,Android应用程序崩溃 - Android app crashes when home button is pressed but orientation change works fine 设置ListAdapter时Android Java App崩溃 - Android Java App crashed when setting up ListAdapter 当 object 添加到 ArrayList 时,Android 应用程序崩溃<object><div id="text_translate"><p>我是新手,因此对任何错误表示歉意。</p><p> 背景:作为 object 培训的一部分,我正在尝试添加 object 练习的 arraylist。 然而。 每次我尝试将 object 练习添加到训练的 Arraylist 时,应用程序都会崩溃。 欢迎任何帮助。</p><p> 对象训练和锻炼看起来像这样</p><pre>public class Training { public Integer trainingNumber; public Date datum; public Integer nrOfExercises; public ArrayList&lt;Exercise&gt; exercises; } public class Training { public Integer trainingNumber; public Date datum; public Integer nrOfExercises; public ArrayList&lt;Exercise&gt; exercises; }</pre><p> 试图生成所有这些的代码如下:</p><pre> private Training createRandomTraining(int i){ Training randomTraining = new Training(); Random random = new Random(); int d = random.nextInt(30)+1; int m = random.nextInt(12)+1; //int y = random.nextInt(21)+2000; int amountOfExercises = random.nextInt(9-6)+6; randomTraining.trainingNumber = i; randomTraining.datum = new Date(2021,m,d); for (int j = 0; j &lt; amountOfExercises; j ++){ Exercise exercise = new Exercise(); Random r = new Random(); int set = r.nextInt(5)+1; int nameInt = r.nextInt(3); int rep = r.nextInt(12)+1; //int y = random.nextInt(21)+2000; exercise.trainingNumber = i; exercise.exerciseNumber = j+1; exercise.exerciseName = getExerciseName(nameInt); exercise.sets = set; exercise.reps = rep; exercise.weight = random.nextInt(200-50)+50; randomTraining.exercises.add(exercise); //the app crashes here. //The other parts work fine. } randomTraining.nrOfExercises = amountOfExercises; return randomTraining; }</pre></div></object> - Android app crashed when object is added to ArrayList<Object> 从一个活动移动到另一个活动时,Android 应用程序崩溃 - Android App crashed when moving from one activity to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM