【问题标题】:UninitializedPropertyAccessException using dagger 2UninitializedPropertyAccessException 使用匕首 2
【发布时间】:2019-11-21 07:46:02
【问题描述】:

我正在尝试向 MainActivity 注入一个 AlertDialog,例如

class MainActivity : BaseActivity() {

    @Inject
    val alertStore:AlertStore;  //propery must be initialized or abstract Error

    private lateinit var viewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

作为 AppComponent.kt

@Component(
    modules = [
        AndroidInjectionModule::class,
        AppModule::class
    ]
)
@Singleton
interface AppComponent : AndroidInjector<App> {

    @Component.Builder
    interface Builder {

        fun addContext(@BindsInstance context: Context): Builder

        fun build(): AppComponent
    }
}

AppModule.kt

@Module
class AppModule {
    @Singleton
    @Provides
    fun provideAlertStore(context: Context) : AlertStore = AlertStore(context)
}

App.kt

class App : DaggerApplication() {

    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        return DaggerAppComponent.builder().addContext(this).build()
    }
}

我是匕首新手。所以请耐心等待。我想我缺少上下文。建议我,请解释一下所以我也理解这个概念

我是否也必须注入 MainActivity 才能获得警报,否则我的上下文丢失了。

生成文件夹下的文件

@kotlin.Metadata(mv = {1, 1, 15}, bv = {1, 0, 3}, k = 1, d1 = {"\u0000&\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\u0018\u00002\u00020\u0001B\u0005\u00a2\u0006\u0002\u0010\u0002J\u0012\u0010\t\u001a\u00020\n2\b\u0010\u000b\u001a\u0004\u0018\u00010\fH\u0014R\u0016\u0010\u0003\u001a\u00020\u00048\u0006X\u0087\u0004\u00a2\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006R\u000e\u0010\u0007\u001a\u00020\bX\u0082.\u00a2\u0006\u0002\n\u0000\u00a8\u0006\r"}, d2 = {"Lcom/example/myapplication/MainActivity;", "Lcom/example/myapplication/common/BaseActivity;", "()V", "alertStore", "Lcom/example/myapplication/common/AlertStore;", "getAlertStore", "()Lcom/example/myapplication/common/AlertStore;", "viewModel", "Lcom/example/myapplication/home/MainViewModel;", "onCreate", "", "savedInstanceState", "Landroid/os/Bundle;", "app_debug"})
public final class MainActivity extends com.example.myapplication.common.BaseActivity {
    @org.jetbrains.annotations.NotNull()
    @javax.inject.Inject()
    private final com.example.myapplication.common.AlertStore alertStore = null;
    private com.example.myapplication.home.MainViewModel viewModel;
    private java.util.HashMap _$_findViewCache;

    @org.jetbrains.annotations.NotNull()
    public final com.example.myapplication.common.AlertStore getAlertStore() {
        return null;
    }

    @java.lang.Override()
    protected void onCreate(@org.jetbrains.annotations.Nullable()
    android.os.Bundle savedInstanceState) {
    }

    public MainActivity() {
        super();
    }
}

在 Alexey 建议后编辑

更新我的变量后

@Inject lateinit var alertStore: AlertStore

错误消失,但在运行应用程序时发生以下错误

方法抛出 'kotlin.UninitializedPropertyAccessException' 异常。

【问题讨论】:

  • @AlexeyRomanov 请参阅我的已编辑问题
  • 这是一个简单的匕首设置stackoverflow.com/a/58009976/2235972
  • 你在super.onCreate之前没有打电话给AndroidInjection.inject(this)...
  • @EpicPandaForce 你是对的。我犯了两个错误,首先我没有添加 ActivityModule,其次我没有调用 AndroidInjection.inject(this)。我怎么能摆脱这个称呼。是否有自动注入的技术或注释

标签: kotlin dependency-injection dagger-2


【解决方案1】:

您的问题与 Dagger 没有直接关系,而是与 Kotlin 直接相关。

既然你使用了val 属性,你需要用一个值来初始化它。否则,当合同声明它是非空的时,它将未初始化。抽象属性显然不起作用,因此您可能需要以不同的方式声明您的属性并使用 setter 注入。

@set:Inject internal lateinit var alertStore: AlertStore

【讨论】:

  • tynn 我已经与@AlexeyRomanov 一起尝试了您的解决方案,但现在发生了 UnintializedPropertyAccessException。请告诉我如何在 AppModule.kt 中将上下文传递给 AlertStore
  • 你创建了ActivityModule吗?
  • 不,我也必须注入 Activity 吗?为什么?以及从哪里 AlertStore 获取 Context 被初始化??
  • 这超出了这个问题的范围。尝试谷歌一些教程。这篇文章似乎是一个好的开始medium.com/@iammert/…
猜你喜欢
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
  • 2015-11-04
  • 2015-01-17
  • 1970-01-01
相关资源
最近更新 更多