我想使用下一个代码为自 4 以来的所有 Android API 应用 SharedPreferences。
/**
* The apply method was introduced
* in Android API level 9.<br> Calling it causes a safe asynchronous write
* of the SharedPreferences.Editor object to be performed. Because
* it is asynchronous, it is the preferred technique for saving SharedPreferences.<br>
* So we should use appropriate method if we doesn`t need confirmation of success.
* In this case we should use old commit method.
*/
@TargetApi(9)
public static void applySharedPreferences(SharedPreferences.Editor editor){
if (Build.VERSION.SDK_INT < 9){
editor.commit();
} else {
editor.apply();
}
}
项目目标 API 为 10(在项目属性中设置)。它在 API 8 上运行良好,但是当我尝试在 API 4 上运行它时,它会因下一条消息而崩溃:
11-18 20:21:45.782: E/dalvikvm(323): Could not find method android.content.SharedPreferences$Editor.apply, referenced from method my.package.utils.Utils.applySharedPreferences
它通常安装在设备上,但在启动过程中崩溃。为什么此 API 中从未使用过此方法(应用)时会发生这种情况?
谢谢