Koin和ViewModel的数据绑定编译错误

时间:2019-12-06 14:26:29

标签: android kotlin android-databinding android-viewmodel koin

我正在尝试将koin插入我现有的项目中。

更新:在@CorroutineDispatcher 的回答之后,我在appModule中添加了一些工厂。我还添加了RepositoryModule

我要在下面声明我的依赖项,以查看是否可以帮助我实施koin:

  • SurvivalViewModel取决于DispatcherGameUseCases(这是由GameInteractor实现的接口)

  • GameInteractor取决于ApplicationDataRepository(这是由DataDownloader

  • 实现的接口)
  • DataDownloader取决于DataAPI

如果您需要更多信息,请告诉我。


这些是我得到的 new 编译错误:

e: C:\Users\joseg\Desktop\Android Course\CapitalCityQuizKtx\app\src\main\java\com\example\capitalcityquizktx\di\AppModule.kt: (6, 55): Unresolved reference: SurvivalViewModel
e: C:\Users\joseg\Desktop\Android Course\CapitalCityQuizKtx\app\src\main\java\com\example\capitalcityquizktx\di\AppModule.kt: (15, 17): Unresolved reference: SurvivalViewModel
e: C:\Users\joseg\Desktop\Android Course\CapitalCityQuizKtx\app\src\main\java\com\example\capitalcityquizktx\di\AppModule.kt: (15, 35): Type inference failed: Not enough information to infer parameter T in inline fun <reified T> get(qualifier: Qualifier? = ..., noinline parameters: ParametersDefinition? /* = (() -> DefinitionParameters)? */ = ...): T
Please specify it explicitly.

e: C:\Users\joseg\Desktop\Android Course\CapitalCityQuizKtx\app\src\main\java\com\example\capitalcityquizktx\di\AppModule.kt: (15, 41): Type inference failed: Not enough information to infer parameter T in inline fun <reified T> get(qualifier: Qualifier? = ..., noinline parameters: ParametersDefinition? /* = (() -> DefinitionParameters)? */ = ...): T
Please specify it explicitly.

SurvivalModeViewModel有两个参数:

class SurvivalViewModel(
    val gameUseCases: GameUseCases,
    private val testDispatcher: CoroutineDispatcher) : ViewModel(), CoroutineScope{

    private val _countries = MutableLiveData<List<Country>>()

    val viewModelJob = Job()

    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + viewModelJob

    override fun onCleared() {
        super.onCleared()
        viewModelJob.cancel()
    }

    private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)

在遵循@CoroutineDispatcher的回答之后,这是我的AppModule

val appModule = module {
    factory { Application() }
    factory { GameInteractor(get(), get()) }
    factory { Dispatchers.Default }
    viewModel { SurvivalViewModel(get(),get()) }
}

这是我的存储库模块:

object RepositoryModule {
    fun getModule() = module {
        single<DataRepository> { DataDownloader(get()) }
        factory { DataApi() }
    }
}

package com.example.capitalcityquizktx

import android.app.Application
import com.example.capitalcityquizktx.di.SurvivalViewModelModule
import com.example.capitalcityquizktx.di.appModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin

class CapitalCityQuizApp : Application() {
    override fun onCreate() {
        super.onCreate()
        startKoin {
            androidLogger()
            androidContext(this@CapitalCityQuizApp)
            modules(listOf(appModule, RepositoryModule.getModule()))
        }
    }
}

这是导致绑定问题的片段

class SurvivalGameFragment : Fragment() {

    private var gameConfig : SurvivalGameConfig? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding : SurvivalGameFragmentBinding = DataBindingUtil.inflate(
            inflater, R.layout.survival_game_fragment, container, false)

        val args = SurvivalGameFragmentArgs.fromBundle(arguments!!)

        if (gameConfig == null)
            gameConfig = args.survivalGameConfig

        Toast.makeText(context, "StartGame", Toast.LENGTH_LONG).show()

        val application = requireNotNull(this.activity).application

        val survivalViewModel by viewModel<SurvivalViewModel>()

        binding.survivalViewModel = survivalViewModel
        binding.setLifecycleOwner(this)

        return null
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="survivalViewModel"
            type="com.example.capitalcityquizktx.ui.survivalmode.SurvivalViewModel"/>
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/survivalGameFragmentConstraint"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".ui.survivalmode.SurvivalGameFragment">

        <TextView
                android:id="@+id/countryTextView"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:text="@string/country"
                android:textSize="24sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.501"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/divider"
                app:layout_constraintVertical_bias="0.21"/>

        <Button
                android:id="@+id/enterBtn"
                style="@style/Widget.AppCompat.Button.Colored"
                android:background="@drawable/button_style_ingame"
                android:textColor="#FFFFFF"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:text="@string/enter"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/capitalEditText"/>

        <View
                android:id="@+id/divider"
                android:layout_width="match_parent"
                android:layout_height="12dp"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:background="?android:attr/listDivider"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintVertical_bias="0.167"
                tools:layout_editor_absoluteX="8dp"/>

        <TextView
                android:id="@+id/counterTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:text="@string/_0"
                android:textAppearance="@style/TextAppearance.AppCompat.Display3"
                android:visibility="visible"
                app:layout_constraintBottom_toTopOf="@+id/divider"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

        <EditText
                android:id="@+id/capitalEditText"
                style="@android:style/Widget.AutoCompleteTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:ems="10"
                android:hint="@string/capital_city"
                android:inputType="textNoSuggestions|textPersonName"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.503"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/countryTextView"/>

        <TextView
                android:id="@+id/timerTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:text="@string/_00_30"
                android:textAppearance="@style/TextAppearance.AppCompat.Display3"
                app:layout_constraintBottom_toTopOf="@+id/divider"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

        <TextView
                android:id="@+id/resultTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:textSize="25sp"
                app:layout_constraintBottom_toTopOf="@+id/countryTextView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/divider"
                tools:visibility="invisible"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.capitalcityquizktx">

    <application
            android:name=".CapitalCityQuizApp"
            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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

</manifest>

1 个答案:

答案 0 :(得分:2)

您尚未在SurvivalViewModelFactory中定义module

val appModule = module {
    factory { GameUseCase() }
    factory { //your dispatcher }
    viewModel { SurvivalViewModel(get(),get()) }

}