将DAO注入活动的问题

时间:2018-02-10 01:50:50

标签: android kotlin dagger-2 rx-java2 android-room

我正在尝试整合Room,dagger 2和rxjava

  
    

io.reactivex.exceptions.OnErrorNotImplementedException:lateinit属性userDao尚未初始化

  

我遇到上述错误,在将活动注入活动后,如何在活动中初始化/访问我的DAO?

我的build.gradle

float

User.kt

// --- Room --- //
implementation "android.arch.persistence.room:runtime:1.1.0-alpha1"
implementation "android.arch.persistence.room:rxjava2:1.1.0-alpha1"
kapt "android.arch.persistence.room:compiler:1.1.0-alpha1"
// --- Room --- //

// --- dagger --- //
implementation "com.google.dagger:dagger:2.14.1"
implementation "com.google.dagger:dagger-android:2.14.1"
implementation "com.google.dagger:dagger-android-support:2.14.1"
kapt "com.google.dagger:dagger-android-processor:2.14.1"
kapt "com.google.dagger:dagger-compiler:2.14.1"

//implementation 'com.google.dagger:dagger:2.14.1'
//kapt 'com.google.dagger:dagger-compiler:2.14.1'
// --- dagger --- //

// --- RxJava2 --- //
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
implementation "io.reactivex.rxjava2:rxkotlin:2.2.0"
// --- RxJava2 --- //

UserDao.kt

import android.arch.persistence.room.ColumnInfo
import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey

@Entity(tableName = "users")
data class User(
    @PrimaryKey
    @ColumnInfo(name = "email") val email: String,
    @ColumnInfo(name = "firstName") val first: String,
    @ColumnInfo(name = "lastName") val last: String
)

AppDatabase.kt

import android.arch.persistence.room.Dao
import android.arch.persistence.room.Insert
import android.arch.persistence.room.OnConflictStrategy
import android.arch.persistence.room.Query
import io.reactivex.Flowable

@Dao interface UserDao {
    @Query("SELECT * FROM users")
    fun getUsers(): Flowable<List<User>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(user: User)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(users: List<User>)
}

AppModule.kt

import android.arch.persistence.room.Database
import android.arch.persistence.room.RoomDatabase

@Database(entities = [(User::class)], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
}

MainActivity.kt

import android.arch.persistence.room.Room
import android.content.Context
import com.singpost.prototype.sam.database.AppDatabase
import dagger.Module
import dagger.Provides
import javax.inject.Singleton

@Module
class AppModule(private val context: Context) {

    @Provides
    @Singleton
    fun provideAppContext() = context

    @Provides
    @Singleton
    fun providesAppDatabase(context: Context): AppDatabase =
        Room.databaseBuilder(context, AppDatabase::class.java, "/data/data/com.lsy.prototype.sam/databases/userdb.db").build()

    @Provides
    fun providesUserDao(appDatabase: AppDatabase) = appDatabase.userDao()
}

2 个答案:

答案 0 :(得分:0)

您应首先创建一个组件以创建图表:

@Component(modules = [
        AndroidInjectionModule::class,
        AndroidSupportInjectionModule::class
        AppModule::class,
        ActivityBuilder::class])
interface ApplicationComponent : AndroidInjector<App> {
    @Component.Builder
    abstract class Builder : AndroidInjector.Builder<App>()
}

这里ActivityBuilder是一个映射我们所有活动的模块,然后Dagger将在编译时了解我们的活动。

@Module
abstract class ActivityBuilder {

    @ContributesAndroidInjector
    abstract fun bindMainActivity(): MainActivity
}

您的应用程序应该扩展DaggerApplication类(或实现HasActivityInjector)并覆盖applicationInjector方法,如下所示:

override fun applicationInjector(): AndroidInjector<App>  
    = DaggerApplicationComponent.builder().create(this)

最后MainActivity应该扩展DaggerAppCompatActivity(或实现HasSupportFragmentInjector,检查在这种情况下如何执行)并且应该注入所有内容。

让我知道它是否有效,也许我错过了一些东西。

答案 1 :(得分:0)

创建组件:

@Component(modules=[AppModule::class])
class AppComponent{
   inject(mainActivity: MainActivity)
}

然后您的Application类可以调用onCreate()

appComponent=DaggerAppComponent.builder().appModule(AppModule(this)).build()

然后你的活动必须通过getter获取Component:

(application as MyApplication).appComponent.inject(this)

你可以使用Gonzalo Acosta的建议来获得更好的Android注入设置,但它比这个最小的设置更复杂。有关Android注入的更多信息,请阅读this