在我的应用程序中,我在 xml 文件夹中创建了设置首选项,并使用该方法将其分配给首选项管理器。但是当我试图从 SharedPreferences 中获取值时,它会抛出空指针异常......
public class RemindMe extends Application {
// private static final String TAG = "RemindMe";
public static SqliteServiceProvider dbHelper;
public static SQLiteDatabase db;
public static SharedPreferences sp;
public static final String TIME_OPTION = "time_option";
public static final String DATE_RANGE = "date_range";
public static final String DATE_FORMAT = "date_format";
public static final String TIME_FORMAT = "time_format";
public static final String VIBRATE_PREF = "vibrate_pref";
public static final String RINGTONE_PREF = "ringtone_pref";
public static final String DEFAULT_DATE_FORMAT = "yyyy-M-d";
@Override
public void onCreate() {
super.onCreate();
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
sp = PreferenceManager.getDefaultSharedPreferences(this);
dbHelper = new SqliteServiceProvider(this);
db = dbHelper.getWritableDatabase();
}
public static boolean showRemainingTime() {
return "1".equals(sp.getString(TIME_OPTION, "0"));
}
public static int getDateRange() {
return Integer.parseInt(sp.getString(DATE_RANGE, "0"));
// return 0;
}
public static String getDateFormat() {
return sp.getString(DATE_FORMAT, DEFAULT_DATE_FORMAT);
}
public static boolean is24Hours() {
return sp.getBoolean(TIME_FORMAT, true);
}
public static boolean isVibrate() {
return sp.getBoolean(VIBRATE_PREF, true);
}
public static String getRingtone() {
return sp.getString(RINGTONE_PREF, android.provider.Settings.System.DEFAULT_NOTIFICATION_URI.toString());
}
}
这是我的设置偏好
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Display Settings">
<ListPreference android:key="time_option"
android:title="Display Time"
android:summary="Actual"
android:entries="@array/time_option_arr"
android:entryValues="@array/val_arr"
android:defaultValue="0" />
<ListPreference android:key="date_range"
android:title="Date Range"
android:summary="Daily"
android:entries="@array/date_range_arr"
android:entryValues="@array/val_arr"
android:defaultValue="0" />
<ListPreference android:key="date_format"
android:title="Date Format"
android:summary="yyyy-M-d"
android:entries="@array/date_format_arr"
android:entryValues="@array/date_format_arr"
android:defaultValue="yyyy-M-d" />
<CheckBoxPreference android:key="time_format"
android:title="Time Format"
android:summary="Use 24-hour format"
android:defaultValue="true" />
</PreferenceCategory>
<PreferenceCategory android:title="Notification Settings">
<CheckBoxPreference android:key="vibrate_pref"
android:title="Vibrate"
android:summary="Vibrate on notification"
android:defaultValue="true" />
<RingtonePreference android:key="ringtone_pref"
android:title="Set Ringtone"
android:summary="Default"
android:ringtoneType="all"
android:showDefault="true"
android:showSilent="false" />
<!--Preference android:key="about_pref"
android:title="About"
android:summary="Credits" /-->
</PreferenceCategory>
</PreferenceScreen>
这是堆栈跟踪...
致命异常:主要 java.lang.RuntimeException:无法恢复活动 {info.androidhive.navigationdrawer/info.androidhive.navigationdrawer.task_classes.AddAlarmActivity}:android.app.ActivityThread.performResumeActivity 的 java.lang.NullPointerException(ActivityThread.java: 2943) 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2972) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2407) 在 android.app.ActivityThread.access$600(ActivityThread.java:162) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368) 在 android.os.Handler.dispatchMessage(Handler.java:107)在 android.os.Looper.loop(Looper.java:194) 在 android.app.ActivityThread.main(ActivityThread.java:5410) 在 java.lang.reflect。com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 的 java.lang.reflect.Method.invoke(Method.java:525) 的 Method.invokeNative(Native Method) com.android。 internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) 原因:java.lang。NullPointerException 在 info.androidhive.navigationdrawer.task_classes.AddAlarmActivity.onResume(AddAlarmActivity.java:147) 在 android.app.Instrumentation.callActivityOnResume(Instrumentation. java:1190) 在 android.app.Activity. 的 android.app.Activity.performResume(Activity.java:5200) 在 android.app.ActivityThread。performResumeActivity(ActivityThread.java:2930) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2972) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2407) at android.app.ActivityThread.access$600(ActivityThread. java:162) 在 android.os.Handler.dispatchMessage(Handler.java: android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)107) 在 android.os.Looper.loop(Looper.java:194) 在 android.app.ActivityThread.main(ActivityThread.java:5410) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang .reflect.Method.invoke(Method.java:525) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 在 com.android。internal.os.ZygoteInit.main(ZygoteInit.java:600) 在 dalvik.system.NativeStart.main(Native Method)