gpt4 book ai didi

android - Dagger 2,在模块中提供应用上下文

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:46 31 4
gpt4 key购买 nike

我在 Android 开发方面很新,在 DI 方面较新。我在一个个人项目上使用 Kotlin,我正在试验 Dagger 2。我设法为一个 util 类设置了它,但是我需要有一个上下文来使用它来注入(inject)一个需要上下文的类(一个 sharedpref 管理器类),我失败了。这是我的代码,这是我收到的错误 ( 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)
}

编辑:添加以下信息,我如何调用 sharedPreferencesManager 的注入(inject):

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 会将应用程序注入(inject)您的所有模块。在您的情况下,只需注入(inject) AppModule

@Singleton
@Component(modules = arrayOf(AppModule::class))
interface AppComponent {
@Component.Builder
interface Builder() {
fun build(): AppComponent

@BindsInstance
fun application(application: Application): Builder
}
}

** 在您的 APP 模块中删除“提供应用程序”功能的代码,并确保您传递应用程序上下文以创建 sharedPreferences。

@Module
class AppModule {


@Provides
@Singleton
fun provideSharedPrefManager(context: Application): SharedPreferencesManager =
SharedPreferencesManager(context)
}

现在在你的 applicationClass 的 onCreate 中

DaggerAppComponent.builder().application(this).build()

可选:如果你想向你的 applicationClass 中注入(inject)一些东西,那么执行以下操作

 DaggerAppComponent.builder().application(this).build().inject(this)

关于android - Dagger 2,在模块中提供应用上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50655722/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com