Dagger2 +Kotlin:lateinit属性尚未初始化

t1qtbnec  于 2023-06-24  发布在  Kotlin
关注(0)|答案(7)|浏览(132)

我尝试将ViewModelFactory注入到我的Activity中,但它一直抛出相同的错误:lateinit属性viewModelFactory尚未初始化。我找不到我可能做错了什么。请参阅上面来自我的类的代码
AppComponent.kt

@Component(modules = [(AppModule::class), (NetworkModule::class), (MainModule::class)])
interface AppComponent {

    fun inject(application: TweetSentimentsApplication)

    fun inject(mainActivity: MainActivity)

    fun context(): Context

    fun retrofit(): Retrofit
}

MainModule.kt

@Module
class MainModule {

    @Provides
    fun mainViewModelFactorty(repository: TweetRepository): MainViewModelFactory = MainViewModelFactory(repository)

    @Provides
    fun local(database: AppDatabase): TweetLocal = TweetLocal(database)

    @Provides
    fun remote(tweetService: TweetService): TweetRemote = TweetRemote(tweetService)

    @Provides
    fun tweetService(retrofit: Retrofit): TweetService = retrofit.create(TweetService::class.java)

    @Provides
    fun repository(local: TweetLocal, remote: TweetRemote): TweetRepository = TweetRepository(local, remote)

}

MainActivity.kt

class MainActivity : AppCompatActivity() {

    @Inject lateinit var viewModelFactory: MainViewModelFactory

    private val viewModel: MainViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        ViewModelProviders.of(this, viewModelFactory).get(MainViewModel::class.java)

        viewModel?.init("guuilp")
        viewModel?.getTweetList()?.observe(this, Observer {
            Toast.makeText(this, it?.size.toString(), Toast.LENGTH_LONG).show()
        })
    }
}

TweetSentimentsApplication.kt

open class TweetSentimentsApplication: Application(){

    companion object {
        lateinit var appComponent: AppComponent
    }

    override fun onCreate() {
        super.onCreate()

        initDI()
    }

    private fun initDI() {
        appComponent = DaggerAppComponent.builder()
                .appModule(AppModule(this))
                .build()
    }
}
oiopk7p5

oiopk7p51#

在初始化MainActivity时,必须调用在AppComponent中定义的inject(mainActivity: MainActivity)方法,这就是Dagger实际注入所需依赖项的方式。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)

    // This is where the dependencies are injected
    TweetSentimentsApplication.appComponent.inject(this)

    ViewModelProviders.of(this, viewModelFactory).get(MainViewModel::class.java)

    ...
}
nimxete2

nimxete22#

另外,请确保您的应用程序名称已添加到AndroidManifest.xml文件中。

<application
    android:name=".YourAppName"
    ..../>
56lgkhnf

56lgkhnf3#

你也可以这样做:

@Inject
  lateinit var viewModelFactory: ViewModelProvider.Factory
  val mainViewModel: MainViewModel by lazy {
      ViewModelProviders.of(this, viewModelFactory)[MainViewModel::class.java]
  }

并使用抽象模块和@ContributesAndroidInjector为Activity,抽象模块为视图模型。使用abstract更有效:

@Module
   abstract class AndroidBindingModule {

   @ContributesAndroidInjector
    internal abstract fun contributesAnActivity(): AnActivity
    }


 @Module
    abstract class ViewModelModule {
      //the default factory only works with default constructor
      @Binds
      @IntoMap
      @ViewModelKey(AViewModel::class)
      abstract fun bindArtViewModel(aViewModel: AViewModel): ViewModel

      @Binds
      abstract fun bindViewModelFactory(factory: AViewModelFactory): ViewModelProvider.Factory
    }



 @Documented
    @Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
    @Retention(RetentionPolicy.RUNTIME)
    @MapKey
    internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
nbnkbykc

nbnkbykc4#

您也可以扩展DaggerAppCompatActivity来代替AppCompatActivity。例如

class MainActivity : DaggerAppCompatActivity() {

    @Inject lateinit var viewModelFactory: MainViewModelFactory

    private val viewModel: MainViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        ViewModelProviders.of(this, viewModelFactory).get(MainViewModel::class.java)

        viewModel?.init("guuilp")
        viewModel?.getTweetList()?.observe(this, Observer {
            Toast.makeText(this, it?.size.toString(), Toast.LENGTH_LONG).show()
        })
    }
}
xkrw2x1b

xkrw2x1b5#

我的错误是创建了一个新对象,并从中获取了一个组件,例如App().component
因此,在这种情况下,您需要将component字段放在companion object中,并将代码替换为App.component

jw5wzhpr

jw5wzhpr6#

我遇到这个问题是因为inject()方法的错误签名,我错误地将参数类型声明为我的类正在实现的接口,而不是类本身。这导致Dagger的inject()实现为空,因此依赖项没有注入->属性没有初始化。
注入依赖项的类:

interface MyInterface {
    ...
}

class MyClass : MyInterface {

    @Inject
    lateinit var someProperty: SomeType

    init {
        ...
        myComponent.inject(this)
        ...
        someProperty.doSomething() // error: lateinit property has not been initialized
    }
}

错误的inject()声明:

interface MyComponent {
    fun inject(x: MyInterface)
}
    • inject()声明中更改参数类型解决了问题。**
interface MyComponent {
    fun inject(x: MyClass)
}
0mkxixxg

0mkxixxg7#

也许你错过了在fragment/activity中实现“Injectable”接口。其将片段/活性标记为可注射的。

相关问题