Android - Shared Preferences crashes on some devices.

柔情痞子 提交于 2019-12-25 08:25:32

问题


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.



来源:https://stackoverflow.com/questions/10491301/android-shared-preferences-crashes-on-some-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!