简体   繁体   English

moxy 和 dagger2 依赖注入问题

[英]moxy and dagger2 dependency injection problem

I'm new in android development and i'm trying to use Moxy and dagger 2 together.我是 android 开发的新手,我正在尝试同时使用 Moxy 和 dagger 2。 I want to inject my presenter into avtivity according to the moxy's documentation.我想根据 moxy 的文档将我的演示者注入 avtivity。 But I got an error lateinit property daggerPresenter has not been initialized because it's null.但是我收到一个错误lateinit 属性 daggerPresenter 尚未初始化,因为它为空。 Here is my Activity code:这是我的活动代码:

package com.example.simpledictionary.activities

import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import androidx.appcompat.widget.SearchView
import com.example.simpledictionary.AndroidApplication
import com.example.simpledictionary.R
import com.example.simpledictionary.di.ApplicationComponent
import com.example.simpledictionary.models.Word
import com.example.simpledictionary.presenters.WordsPresenter
import com.example.simpledictionary.views.WordsView
import kotlinx.android.synthetic.main.activity_main.*
import moxy.MvpAppCompatActivity
import moxy.presenter.InjectPresenter
import moxy.presenter.ProvidePresenter
import javax.inject.Inject

class DictionaryActivity : MvpAppCompatActivity(), WordsView {
    @InjectPresenter
    lateinit var wordsPresenter: WordsPresenter

    @Inject
    lateinit var daggerPresenter: WordsPresenter

    @ProvidePresenter
    fun providePresenter(): WordsPresenter {
        //why it's empty?
        return daggerPresenter
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val appComponent: ApplicationComponent = (application as AndroidApplication).appComponent
        appComponent.inject(this)
//if I'll comment @ProvidePresenter annotation, there will be not null there
        Log.i("TAG", daggerPresenter.toString())
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.search_word_menu, menu)
        val menuItem = menu.findItem(R.id.search_word)
        if (menuItem != null) {
            val view = menuItem.actionView as SearchView;

            view.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
                override fun onQueryTextSubmit(query: String): Boolean {
//                    wordsPresenter.searchWords(query)
//                    Log.i("TAG", wordsPresenter.toString())
                    return true;
                }

                override fun onQueryTextChange(newText: String?): Boolean {
                    return false
                }
            })
        }

        return true
    }

    override fun showError(textResource: Int) {
        txtFriendsNoItems.text = getString(textResource)
    }

    override fun showWords(words: List<Word>) {
        TODO("Not yet implemented")
    }
}

When I pointed a breakpoint at "return daggerPresenter" line I saw that daggerPresenter is empty.当我在“return daggerPresenter”行指向断点时,我看到 daggerPresenter 是空的。 But if I will remove @ProvidePresenter annotation and look at daggerPresenter variable, let's say in onCreate method like this way:但是,如果我将删除@ProvidePresenter 注释并查看daggerPresenter变量,让我们在 onCreate 方法中这样说:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val appComponent: ApplicationComponent = (application as AndroidApplication).appComponent
        appComponent.inject(this)

        Log.i("TAG", daggerPresenter.toString())
    }
I will see, that i's not empty!

It seems like @ProvidePresenter annotation works BEFORE dagger's @Inject annotation... Dagger works fine itself.似乎 @ProvidePresenter 注释在 dagger 的 @Inject 注释之前起作用...... Dagger 本身工作正常。

Here is my rest code这是我的休息代码

ApplicationComponent

package com.example.simpledictionary.di

import com.example.simpledictionary.AndroidApplication
import com.example.simpledictionary.activities.DictionaryActivity
import dagger.Component
import javax.inject.Singleton

@Singleton
@Component(modules = [ApplicationModule::class])
interface ApplicationComponent {
    fun inject(application: AndroidApplication)
    fun inject(dictionaryActivity: DictionaryActivity)
}

ApplicationModule应用模块

package com.example.simpledictionary.di

import android.content.Context
import com.example.simpledictionary.AndroidApplication
import com.example.simpledictionary.presenters.WordsPresenter
import dagger.Module
import dagger.Provides
import javax.inject.Singleton

@Module
class ApplicationModule(private val application: AndroidApplication) {
    @Provides
    @Singleton
    fun provideApplicationContext(): Context = application

    @Provides
    fun provideWordsPresenter(): WordsPresenter {
        return WordsPresenter()
    }
}

AndroidApplication安卓应用

package com.example.simpledictionary

import android.app.Application
import android.util.Log
import com.example.simpledictionary.di.ApplicationComponent
import com.example.simpledictionary.di.ApplicationModule
import com.example.simpledictionary.di.DaggerApplicationComponent

class AndroidApplication : Application() {
    lateinit var appComponent: ApplicationComponent

    override fun onCreate() {
        super.onCreate()
        Log.i("TAG", "created")
        setup()
        injectMembers()
    }

    private fun setup() {
        appComponent = DaggerApplicationComponent
            .builder()
            .applicationModule(ApplicationModule(this))
            .build()
    }

    private fun injectMembers() = appComponent.inject(this)
}

Android manifest:安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simpledictionary">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name=".AndroidApplication"
        >
        <activity android:name=".activities.DictionaryActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

gradle configuration:梯度配置:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.simpledictionary"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}
kapt {
    generateStubs = true
}
def moxyVersion = "2.1.2"
def daggerVersion = 2.27
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha01'
    implementation "com.github.moxy-community:moxy:$moxyVersion"
    implementation "com.github.moxy-community:moxy-app-compat:$moxyVersion"
    kapt "com.github.moxy-community:moxy-compiler:$moxyVersion"
    implementation "com.google.dagger:dagger:$daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
}

What did I wrong?我做错了什么? Excuse me for my bad English.请原谅我的英语不好。

The @ProvidePresenter method is called in super.onCreate(savedInstanceState) . @ProvidePresenter方法在super.onCreate(savedInstanceState)调用。 In order to access daggerPresenter at this time, you need to inject it before calling super.onCreate .此时为了访问daggerPresenter ,需要在调用super.onCreate之前注入。

override fun onCreate(savedInstanceState: Bundle?) {
    val appComponent: ApplicationComponent = (application as AndroidApplication).appComponent
    appComponent.inject(this)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    Log.i("TAG", daggerPresenter.toString())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM