gpt4 book ai didi

java - 某些设备上的 getSharedPreferences 空指针异常

转载 作者:行者123 更新时间:2023-11-30 02:33:52 25 4
gpt4 key购买 nike

我正在使用 SharedPreferences 来获取一些保存的值。我只在某些设备上得到空指针异常。当我在自己的设备中测试代码时,我没有遇到任何问题。但是我的一些用户收到了错误。

这是我获取 SharedPreference 值的类:

public class SavingData {
public static final String PREFS_NAME = "MyPrefsFile";
public static MainActivity mainActivity;

public static int getRestTime() {
// Restore preferences
SharedPreferences settings = mainActivity.getSharedPreferences(PREFS_NAME, 0);
int restTime = settings.getInt("resttime", defaultRestTime); // 0 is the default
// value
return restTime;
}

在我的主类中,我声明了 mainActivity 变量。

  SavingData.mainActivity = this;

这是我得到的错误:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.playsimple.fitnessapp/com.playsimple.fitnessapp.ExerciseStartActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) at android.app.ActivityThread.access$800(ActivityThread.java:139) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5086) 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:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.playsimple.fitnessapp.data.SavingData.getDifficulty(SavingData.java:91) at com.playsimple.fitnessapp.ExerciseStartActivity.initExercise(ExerciseStartActivity.java:69) at com.playsimple.fitnessapp.ExerciseStartActivity.onCreate(ExerciseStartActivity.java:64) at android.app.Activity.performCreate(Activity.java:5248) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) ComponentInfo{com.playsimple.fitnessapp/com.playsimple.fitnessapp.ExerciseStartActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) at android.app.ActivityThread.access$1500(ActivityThread.java:117) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3683) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.playsimple.fitnessapp.data.SavingData.getDifficulty(SavingData.java:91) at com.playsimple.fitnessapp.ExerciseStartActivity.initExercise(ExerciseStartActivity.java:69) at com.playsimple.fitnessapp.ExerciseStartActivity.onCreate(ExerciseStartActivity.java:64) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

最佳答案

您正在此处创建主要 Activity 的实例:

public static MainActivity mainActivity;

然后调用共享的 Prefs.. 使用它。从您的主要 Activity 中获取上下文对象,然后调用它。还使用这个包括 MODE_PRIVATE 参数:

SharedPreferences userDetails = context.getSharedPreferences("userdetails", MODE_PRIVATE);

参见 this detailed answer .另请参阅 [Context getSharedPreferences]( http://developer.android.com/reference/android/content/Context.html#getSharedPreferences(java.lang.String , int) )

的文档

关于java - 某些设备上的 getSharedPreferences 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26893971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com