您好我正在学习Android一段时间,我想制作一个有更多活动的应用程序,一个是提醒,我已设法清理错误代码但是当我在手机上运行应用程序并选择该活动时,它崩溃,这是日志:
10-12 16:54:12.485 5364-5364/com.mihai_bucur.happycar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mihai_bucur.happycar, PID: 5364
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mihai_bucur.happycar/com.mihai_bucur.happycar.Documente}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference
at com.mihai_bucur.happycar.RemindMe.getDateRange(RemindMe.java:51)
at com.mihai_bucur.happycar.Documente.onCreate(Documente.java:69)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
此处还有与崩溃相关的活动:
RemindMe.java
package com.mihai_bucur.happycar;
import android.app.Application;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import com.mihai_bucur.happycar.model.DbHelper;
/**
* Created by Mihai on 03.10.2016.
*/
public class RemindMe extends Application {
// private static final String TAG = "RemindMe";
public static DbHelper 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.setari, false);
sp = PreferenceManager.getDefaultSharedPreferences(this);
dbHelper = new DbHelper(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"));
}
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());
}
}
在getDateRage行出现错误
并且也出现在我的主要活动上的创建方法在" int r = RemindMe.getDateRange();"
Documente.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_documente);
findViews();
db = RemindMe.db;
/*font = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Semibold.ttf");*/
headingText.setTypeface(font);
monthArr = getResources().getStringArray(R.array.spinner3_arr);
int r = RemindMe.getDateRange();
你可以帮我解决这个问题吗? :D,我在互联网上搜索过但我无法找到解决方案并使其正常工作
谢谢!