我在注入偏好时遇到错误:
无法解决问题。
以下是我正在使用的一些课程:
AppModule类
@Module
public class AppModule {
private final MyApplication app;
public AppModule(MyApplication app) {
this.app = app;
}
@Provides
@Singleton
public Context provideContext() {
return app;
}
@Provides
@Singleton
SharedPreferences providesSharedPreference() {
return PreferenceManager.getDefaultSharedPreferences(app);
}
}
// The AppComponent interface
@Singleton
@Component(
modules = {
AppModule.class,
}
)
public interface AppComponent {
void inject(DisplayFragment fragment);
}
申请类
public class MyApplication extends Application {
private static MyApplication instance = new MyApplication();
private static AppComponent appComponent;
public static MyApplication getInstance() {
return instance;
}
@Override
public void onCreate() {
super.onCreate();
getAppComponent();
}
public AppComponent getAppComponent() {
if (appComponent == null) {
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
return appComponent;
}
}
最后在我正在做的一个片段中:
public class DisplayFragment extends Fragment implements View.OnClickListener {
...
@Inject
SharedPreferences prefs;
@Inject
public DisplayFragment() {
MyApplication.getInstance().getAppComponent().inject(this);
}
..
}
我收到此错误,应用程序崩溃。
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133)
at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:390)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:385)
at dagger.module.AppModule.providesSharedPreference(AppModule.java:34)
at dagger.module.AppModule_ProvidesSharedPreferenceFactory.get(AppModule_ProvidesSharedPreferenceFactory.java:23)
at dagger.module.AppModule_ProvidesSharedPreferenceFactory.get(AppModule_ProvidesSharedPreferenceFactory.java:8)
at dagger.internal.ScopedProvider.get(ScopedProvider.java:47)
at ui.DisplayFragment_MembersInjector.injectMembers(DisplayFragment_MembersInjector.java:29)
at ui.DisplayFragment_MembersInjector.injectMembers(DisplayFragment_MembersInjector.java:8)
at dagger.component.DaggerAppComponent.inject(DaggerAppComponent.java:45)
at ui.DisplayFragment.<init>(DisplayFragment.java:37)
at ui.DisplayFragment.newInstance(DisplayFragment.java:41)
at ui.MainActivity$MyPagerAdapter.getItem(MainActivity.java:86)
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:101)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1182)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:19324)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:825)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:511)
at android.view.View.measure(View.java:19324)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19324)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:19324)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at android.view.View.measure(View.java:19324)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:788)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:648)
at android.view.View.measure(View.java:19324)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2694)
at android.view.View.measure(View.java:19324)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2545)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1454)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1709)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1335)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6816)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
at android.view.Choreographer.doCallbacks(Choreographer.java:696)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5896)
at ja
有人请帮忙..
答案 0 :(得分:1)
您可能需要将providesSharedPreference()
更改为:
@Provides
@Singleton
SharedPreferences providesSharedPreference(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
并在onCreateView()
方法中执行注入,因为当调用片段构造函数时,当前设置无法保证Context
已初始化。
答案 1 :(得分:1)
我不确定您的应用程序单例是如何工作的(MyApplication instance = new MyApplication()
),因为Android会为您创建应用程序。这也是导致崩溃的原因:您将自制的,未初始化的实例传递给PreferenceManager。
在你的片段中,尝试注入onCreateView()
,例如,像这样:
public class DisplayFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
((MyApplication) getActivity().getApplication()).getAppComponent().inject(this);
return ...; // your fragment view
}
}