In my application i have create the settings preference in xml folder and assigned it to preference manger using the method. But when im trying to get values from the SharedPreferences it's throwing null pointer exception....
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());
}
}
and here is my settings preferrence
<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>
here is the stacktrace...
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to resume activity {info.androidhive.navigationdrawer/info.androidhive.navigationdrawer.task_classes.AddAlarmActivity}: java.lang.NullPointerException at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2943) 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) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5410) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at info.androidhive.navigationdrawer.task_classes.RemindMe.getDateFormat(RemindMe.java:53) at info.androidhive.navigationdrawer.task_classes.AddAlarmActivity.onResume(AddAlarmActivity.java:147) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1190) at android.app.Activity.performResume(Activity.java:5200) at 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) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5410) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method)
Did you forget adding the the name of your Application
class in your AndroidManifest.xml?
<manifest
...
<application
name=".RemindMe"
...
</application>
</manifest>