Android - Shared Preferences crashes on some devic

2019-09-18 10:27发布

问题:

I have this exception that I get in my crash reports from Android:

java.lang.NoSuchMethodError: android.content.SharedPreferences$Editor.apply
at com.problemio.ProblemioActivity.first_time_check(ProblemioActivity.java:231)
at com.problemio.ProblemioActivity.onCreate(ProblemioActivity.java:47)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4668)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)

I am pretty new to the crash reporting system in Android. Why would this crash happen? I can not reproduce it on my device or the simulator.

What other info is needed from the crash report to determine the cause and how to fix this?

Thanks!

回答1:

apply() was introduced in API level 9, and some devices that runs your app may be with lower version. consider changing this call to commit. As stated in the docs:

The SharedPreferences.Editor interface isn't expected to be implemented directly. However, if you previously did implement it and are now getting errors about missing apply(), you can simply call commit() from apply().



回答2:

Read the documentation:

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()

Look at the api version that it was started with.