0
我有一個MainActivity和類共享首選項。ut空對象引用上的共享首選項有什麼問題?
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (DataStorage.getConfig(AppCore.getContext(),Const.FIRST).equals("true")){
FragmentManager manager = getSupportFragmentManager();
FirstDialog firstDialog = new FirstDialog();
firstDialog.show(manager,"dialog");
DataStorage.setConfig(AppCore.getActivity(), Const.FIRST,"false");
}
}
}
而且具有共享偏好的數據存儲類:
public class DataStorage {
public static String getConfig(Context context, String name) {
return getConfig(context, name, "");
}
public static String getConfig(Context context, String name, String defValue) {
SharedPreferences settings = context.getSharedPreferences(
Const.NAME, 0);
String ret = settings.getString(name, defValue);
return ret;
}
public static void setConfig(Context context, String name, String value) {
SharedPreferences settings = context.getSharedPreferences(
Const.NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(name, value);
editor.commit();
}
}
所以我是說,一個錯誤水木清華這樣的:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: nortti.ru.dogshelter, PID: 16246
java.lang.RuntimeException: Unable to start activity ComponentInfo{nortti.ru.dogshelter/nortti.ru.dogshelter.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2326)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5264)
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:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at nortti.ru.dogshelter.DataStorage.getConfig(DataStorage.java:15)
at nortti.ru.dogshelter.DataStorage.getConfig(DataStorage.java:11)
at nortti.ru.dogshelter.activities.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5264)
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:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
它的接縫是womething錯在上下文數據存儲。
您應該使DataStorage成爲使用應用程序上下文的單例,而不是處理活動上下文 –