fun provideRequestOptions(): RequestOptions {
return RequestOptions()
.placeholder(R.drawable.white_background)
.error(R.drawable.white_background)
@Provides
fun provideGlideInstance(application: Application, requestOptions: RequestOptions): RequestManager{
return Glide.with(application).setDefaultRequestOptions(requestOptions)
@Nullable
@Provides
fun provideAppDrawable(application: Application): Drawable? {
return ContextCompat.getDrawable(application, R.drawable.logo)
和AuthActivity。
class AuthActivity : DaggerAppCompatActivity() {
lateinit var binding: ActivityAuthBinding
@Inject
lateinit var logo: Drawable
@Inject
lateinit var requestManager: RequestManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAuthBinding.inflate(layoutInflater)
setContentView(binding.root)
setLogo()
private fun setLogo() {
requestManager.load(logo).into(binding.loginLogo)
AppModule中的provideAppDrawable()
必须返回nullableDrawable?
。当我试图构建应用程序时,Dagger抱怨说它是无效的。
\AppComponent.java:7: error: [Dagger/Nullable] android.graphics.drawable.Drawable is not nullable, but is being provided by @org.jetbrains.annotations.Nullable @androidx.annotation.Nullable @Provides android.graphics.drawable.Drawable com.example.daggercodingwithmitch.di.AppModule.provideAppDrawable(android.app.Application)
public abstract interface AppComponent extends dagger.android.AndroidInjector<com.example.daggercodingwithmitch.BaseApplication> {
首先,我试着让logo
中的var变成nullable,但latinit不能变成nullable。如果我不使用lateinit,并做出这样的事情。【替换代码7我就会得到关于私有字段注入的奇怪错误,尽管它不是私有的。
\AuthActivity.java:10: error: Dagger does not support injection into private fields
private android.graphics.drawable.Drawable logo;
我怎样才能修复它?谢谢。
3 个回答
0 人赞同
使标志字段成为可忽略的。Dagger不允许向非nullable字段注入nullable对象。
@Inject
var logo: Drawable?=null
在Java中。
@Nullable
@Inject
Drawable logo;