带参数的Dagger2注入类(使用Room) [英] Dagger2 Inject class with parameter (using Room)

查看:166
本文介绍了带参数的Dagger2注入类(使用Room)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用Dagger2注入类时遇到问题.我正在使用RoomDatabase进行数据库访问.

I have a problem with injecting classes with Dagger2. I am using RoomDatabase for database access.

我的房间设置:

My room setup:

Dao的

interface noteDao()
interface noteTypeDao()
interface userDao()

NoteRepository

@Singleton
class NoteRepository @Inject constructor(
    private val noteDao: NoteDao,
    private val noteTypeDao: NoteTypeDao,
    private val userDao: UserDao
) {

}

AppDatabase

@Database(entities = [Note::class, User::class, NoteType::class], version = 1)
abstract class AppDatabase : RoomDatabase() {

    abstract fun noteDao(): NoteDao
    abstract fun userDao(): UserDao
    abstract fun noteTypeDao(): NoteTypeDao

    companion object {
        @Volatile
        private var INSTANCE: AppDatabase? = null

        fun getDatabase(context: Context): AppDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "NoteDatabase"
                ).build()
                INSTANCE = instance
                return instance
            }
        }
    }
}

匕首2设置:

Dagger 2 setup:

AppModule

@Module
class AppModule {

    @Provides
    fun provideNoteRepository(app: Application): NoteRepository {
        return NoteRepository(
            AppDatabase.getDatabase(app).noteDao(),
            AppDatabase.getDatabase(app).noteTypeDao(),
            AppDatabase.getDatabase(app).userDao()
        )
    }

    @Provides
    fun provideApplication(): Application {
        return Application()
    }

}

AppComponent

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

我在context.applicationContext行的AppDatabase中得到了一个N​​ullPointerExeption int.建议如何解决问题? 似乎AppDatabase无法从Dagger2获取应用程序实例.

I am getting a NullPointerExeption int the AppDatabase in the line context.applicationContext. Any suggetion how to solve the problem? It seems that the AppDatabase doesnt get the application instance from Dagger2.

推荐答案

Application是框架类,您不能只通过调用其构造函数来实例化它.相反,您需要将框架实例化的应用程序传入模块中,并提供:

Application is a framework class, you can not just instantiate it yourself by calling its constructor. Instead, you need to pass in your application that the framework instantiates for you into your module, and provide that:

@Module
class AppModule(val application: Application) { 
    ...

    @Provides
    fun provideApplication(): Application {
        return application
    }
}

现在,如果您以前在应用程序的onCreate中创建像这样的AppComponent(大概是这样做的通常方法):

Now, if you were creating your AppComponent like this before, in your application's onCreate (presumably, as that's the usual way to do it):

override fun onCreate() {
    injector = DaggerAppComponent.create()
}    

您必须将其替换为以下内容,将您的应用程序实例传递给模块,以便随后可以提供它:

You'd have to replace it with something like this, passing in your application instance to the module so that it can then provide it:

override fun onCreate() {
    injector = DaggerAppComponent.builder()
            .appModule(appModule(this))
            .build()
}

这篇关于带参数的Dagger2注入类(使用Room)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆