解决方法
在AndroidManifest中配置Application的名字
前因
在使用自己定义的Application中初始化Sharepreferences总是提示空指针异常
package com.xinzhi.zhouzhi.model.prefs;
import android.content.Context;
import android.content.SharedPreferences;
import com.xinzhi.zhouzhi.application.MyApplication;
import javax.inject.Inject;
public class PreferencesHelperImpl implements PreferencesHelper {
private SharedPreferences sharedPreferences;
private static final String SP_NAME="my_sp";
@Inject
public PreferencesHelperImpl() {
sharedPreferences = MyApplication.getInstance().getSharedPreferences(SP_NAME, Context.
MODE_PRIVATE);
}
@Override
public void setToken(String token) {
sharedPreferences.edit().putString("token",token).commit();
}
@Override
public String getToken() {
return sharedPreferences.getString("token","");
}
@Override
public void setUserId(String userId) {
sharedPreferences.edit().putString("userId",userId).commit();
}
@Override
public String getUserId() {
return sharedPreferences.getString("userId","");
}
}
后果
Process: com.xinzhi.zhouzhi, PID: 1994
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xinzhi.zhouzhi/com.xinzhi.zhouzhi.page.splash.SplashActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
at com.xinzhi.zhouzhi.model.prefs.PreferencesHelperImpl.<init>(PreferencesHelperImpl.java:17)
at com.xinzhi.zhouzhi.model.prefs.PreferencesHelperImpl_Factory.get(PreferencesHelperImpl_Factory.java:15)
at com.xinzhi.zhouzhi.model.prefs.PreferencesHelperImpl_Factory.get(PreferencesHelperImpl_Factory.java:6)
at com.xinzhi.zhouzhi.dagger.Module.AppModule_ProvideDataManagerModelFactory.get(AppModule_ProvideDataManagerModelFactory.java:40)
at com.xinzhi.zhouzhi.dagger.Module.AppModule_ProvideDataManagerModelFactory.get(AppModule_ProvideDataManagerModelFactory.java:12)
at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
at com.xinzhi.zhouzhi.dagger.component.DaggerAppComponent.getDataManagerModel(DaggerAppComponent.java:126)
at com.xinzhi.zhouzhi.dagger.component.DaggerActivityComponent.getSplashPresenter(DaggerActivityComponent.java:54)
at com.xinzhi.zhouzhi.dagger.component.DaggerActivityComponent.injectSplashActivity(DaggerActivityComponent.java:106)
at com.xinzhi.zhouzhi.dagger.component.DaggerActivityComponent.inject(DaggerActivityComponent.java:87)
at com.xinzhi.zhouzhi.page.splash.SplashActivity.setInject(SplashActivity.java:23)
at com.xinzhi.zhouzhi.base.MvpBaseActivity.onCreateView(MvpBaseActivity.java:32)
at com.xinzhi.zhouzhi.base.BaseActivity.onCreate(BaseActivity.java:33)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)