我在Android开发中相当新,在DI中更新。我正在一个个人项目中使用 Kotlin,在那里我正在试验 Dagger 2。我设法为一个 util 类设置了它,但是我需要有一个上下文来使用它来注入一个需要上下文的类(共享pref 管理器类(,我失败了。这是我的代码,这是我收到的错误(NPE(。提前谢谢你。
我的模块类
package com.android.pine
import android.content.Context
import com.android.pine.utils.SharedPreferencesManager
import dagger.Module
import dagger.Provides
import javax.inject.Singleton
@Module
class AppModule {
@Provides
@Singleton
fun context(pineApplication: PineApplication): Context = pineApplication.applicationContext
@Provides
@Singleton
fun provideSharedPrefManager(context: Context): SharedPreferencesManager = SharedPreferencesManager(context)
}
我的组件类:
package com.android.pine
import com.android.pine.home.HomePresenter
import com.android.pine.home.categories.CategoryAdapter
import dagger.Component
import javax.inject.Singleton
@Singleton
@Component(modules = arrayOf(AppModule::class))
interface AppComponent {
fun inject(categoryAdapter: CategoryAdapter)
fun inject(homePresenter: HomePresenter)
}
编辑:添加了以下信息, 我如何调用共享首选项管理器的注入:
class HomePresenter : BasePresenter<HomeView>() {
@Inject
lateinit var sharedPreferencesManager: SharedPreferencesManager
.
.
.
同样在我的 HomePresenter 类中,在 onAttached 方法覆盖中:
DaggerAppComponent.create().inject(this)
我的pineApplication类和SharedPrefManager类看起来像这样:
class PineApplication @Inject constructor(): Application()
共享首选项:
class SharedPreferencesManager @Inject constructor(context: Context) {
.
.
.
崩溃,无法获取pineApplication.getContext(((编辑,添加了全栈跟踪(
06-02 11:57:01.028 14840-14840/com.android.pine.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.pine.debug, PID: 14840
java.lang.RuntimeException: Unable to resume activity {com.android.pine.debug/com.android.pine.home.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3429)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3469)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
at com.android.pine.AppModule.context(AppModule.kt:12)
at com.android.pine.AppModule_ContextFactory.proxyContext(AppModule_ContextFactory.java:34)
at com.android.pine.DaggerAppComponent.getContext(DaggerAppComponent.java:29)
at com.android.pine.DaggerAppComponent.getSharedPreferencesManager(DaggerAppComponent.java:34)
at com.android.pine.DaggerAppComponent.injectHomePresenter(DaggerAppComponent.java:59)
at com.android.pine.DaggerAppComponent.inject(DaggerAppComponent.java:49)
at com.android.pine.home.HomePresenter.onAttached(HomePresenter.kt:31)
at com.android.pine.home.HomePresenter.onAttached(HomePresenter.kt:10)
at com.android.pine.core.BaseActivity.onResume(BaseActivity.kt:34)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
at android.app.Activity.performResume(Activity.java:6783)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3406)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3469)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这就是它是如何完成的。在组件中使用@BindsInstance会将应用程序注入所有模块。在您的情况下,只需应用模块
@Singleton
@Component(modules = arrayOf(AppModule::class))
interface AppComponent {
@Component.Builder
interface Builder() {
fun build(): AppComponent
@BindsInstance
fun application(application: Application): Builder
}
}
** 删除 APP 模块中"提供应用程序"功能的代码,并确保传递应用程序上下文以创建共享首选项。
@Module
class AppModule {
@Provides
@Singleton
fun provideSharedPrefManager(context: Application): SharedPreferencesManager =
SharedPreferencesManager(context)
}
现在在你的创建应用程序类
DaggerAppComponent.builder().application(this).build()
可选:如果要将某些内容注入到应用程序类中,请执行以下操作
DaggerAppComponent.builder().application(this).build().inject(this)
不能使用class PineApplication @Inject constructor(): Application()
创建PineApplication
。它是一个框架类,必须由 Android Framework 创建。
这样做 Dagger 将创建PineApplication
,但applicationContext
将返回null
因为它从未被初始化(由系统(。
不要对框架类使用构造函数注入,也不要自己创建。使用@Bindsintance
将对象及其生成器添加到组件中,或使用模块提供对象。
您可以尝试像这样修改应用程序模块。
@Module
class ApplicationModule(private val application: Application) {
@Provides
@Singleton
fun provideContext(): Context {
return application.applicationContext
}
@Provides
@Singleton
fun provideSharedPreferences(context: Context): SharedPreferences {
return context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE)
}
}
然后,您可以从应用程序类中生成如下所示的dagger组件。
val appComponent = DaggerAppComponent.builder()
.applicationModule(ApplicationModule(this))
.build()
像这样在演示器中注入值。
application.appComponent.inject(this)
对于提供应用程序上下文,您可以创建类,例如ComponentsManager
跟:
public class ComponentsManager {
private Context context;
private AppComponent appComponent;
public ComponentsManager(Context context) {
this.context = context.getApplicationContext();
}
public AppComponent getAppComponent(){
if (appComponent == null){
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(context))
.build();
}
return appComponent;
}
}
在您的应用程序类中,初始化此ComponentsManager
如下所示:
public class YourApp extends Application {
private static ComponentsManager componentsManager;
@Override
public void onCreate() {
super.onCreate();
initComponentsManager();
initAppComponent();
}
public static ComponentsManager getComponentsManager(){
return componentsManager;
}
private void initComponentsManager(){
componentsManager = new ComponentsManager(this);
}
private void initAppComponent(){
componentsManager.getAppComponent();
}
}