Я новичок в android разработке, и я пытаюсь использовать Moxy и Dagger 2 вместе. Я хочу ввести своего докладчика в работу в соответствии с документацией Мокси. Но я получил сообщение об ошибке свойство lateinit daggerPresenter не было инициализировано , потому что оно пустое. Вот мой код активности:
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")
}
}
Когда я указал точку останова на строку «return daggerPresenter», я увидел, что daggerPresenter пуст. Но если я удалю аннотацию @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!
Похоже, Аннотация @ProvidePresenter работает ДО @Nject-аннотации кинжала ... Кинжал работает нормально.
Вот мой код отдыха
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 манифест:
<?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:
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"
}
Что я не так сделал? Извините за мой плохой Энгли sh.