我正在尝试用于依赖注入的Toothpick库,它看起来比其他库更易于使用和测试。

但是我在运行代码时遇到一个问题,Toothpick不会注入任何东西。我现在正在使用它,很难弄清楚。

我正在使用Kotlin,Android Studio 2.3.3和Gradle 2.3.3,这是我的代码:

build.gradle

//KOTLIN_VERSION=1.1.4
//TOOTHPICK_VERSION=1.0.8

compile "com.github.stephanenicolas.toothpick:toothpick-runtime:$TOOTHPICK_VERSION"
    compile "com.github.stephanenicolas.toothpick:smoothie:$TOOTHPICK_VERSION"
    kapt "com.github.stephanenicolas.toothpick:toothpick-compiler:$TOOTHPICK_VERSION"


class AppModule : Module {
    constructor(application: Application) {
        bind(QuestionRepository::class.java).toInstance(QuestionRepository(application))
    }
}

class MyApp : Application() {

    override fun onCreate() {
        super.onCreate()
        setupInjector()
    }

    fun setupInjector() {
        val appScope = Toothpick.openScope(this)
        appScope.installModules(SmoothieApplicationModule(this), AppModule(this))

        if (BuildConfig.DEBUG) {
            Toothpick.setConfiguration(Configuration.forDevelopment());
        }
    }
}

class MainViewModule : Module {
    constructor() {
        bind(MainPresenter::class.java).to(MainPresenter::class.java)
    }
}

class QuestionRepository @Inject constructor(application: Application) {
    val assetManager: AssetManager = application.assets

    //a couple of functions
}

class MainActivity : AppCompatActivity(), MainView,
        NavigationView.OnNavigationItemSelectedListener {

    @Inject lateinit var presenter: MainPresenter

    lateinit private var activityScope: Scope

    val binding: ActivityMainBinding by lazy {
        DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        setUpInject()
        super.onCreate(savedInstanceState)
        setUpView()
    }

    override fun onDestroy() {
        Toothpick.closeScope(activityScope)
        super.onDestroy()
    }

    fun setUpInject() {
        activityScope = Toothpick.openScopes(application, this)
        activityScope.installModules(MainViewModule())
        Toothpick.inject(this, activityScope)

        //println(activityScope.toString())

        presenter.onAttachView(this)
    }

    fun setUpView() {
        //
    }

    //Omit implemented method of OnNavigationItemSelectedListener
}

class MainPresenter @Inject constructor() {}


错误信息:

kotlin.UninitializedPropertyAccessException: lateinit property presenter has not been initialized


当我在MainActivity中打印activityScope时显示:

 Providers: [com.ispark.app.MainPresenter,toothpick.Scope]


当我检查KotlinWeather示例时,该代码未在活动中安装任何模块,但仍注入依赖项。我不知道它是如何工作的。
无论如何,我对Toothpick还是很陌生,我想念什么?

感谢您的帮助。

编辑1:

在build.gradle中,我用注解处理器“ com.github.stephanenicolas.toothpick:toothpick-compiler:$ TOOTHPICK_VERSION”替换了kapt,但仍然相同。

AndroidBinding库是否有任何问题?

编辑2:

activityScope日志不是针对activityScope的,而是来自Toothpick的日志。
activityScope是activityScope:com.ispark.app.MainActivity@2a0e3388:303497268

最佳答案

您需要确保在使用演示者的代码中调用了Toothpick.inject(this,scope)。拥有一些正在使用的代码示例也将有所帮助。我还建议您查看牙签项目的示例代码,它应该可以助您一臂之力。您还需要创建范围或检索现有范围以用于注入。

10-07 19:30