UninitializedPropertyAccessException:lateinit属性首选项尚未初始化 [英] UninitializedPropertyAccessException: lateinit property pref has not been initialized

查看:1156
本文介绍了UninitializedPropertyAccessException:lateinit属性首选项尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经在此处回答了类似的问题.但这是由于黄油刀库问题引起的,但我的情况有所不同.就我而言,当我在基本活动中使用注入匕首的属性时,会显示错误由以下原因引起:kotlin.UninitializedPropertyAccessException:lateinit属性首选项尚未初始化

I know a similar question has been answered Here. But that was due to butter knife library problem but my case is different. In my case when I use dagger injected properties in my base activity it shows me error Caused by: kotlin.UninitializedPropertyAccessException: lateinit property pref has not been initialized

但是当我在子活动(登录活动)中使用相同的属性时,它可以正常工作.

But the same property when I use in my sub activity (Login activity) it works fine.

例如 pref.setLanguage("abc")->在登录活动中工作正常,但在基本活动中抛出错误

这是我的代码:

BaseActivity

    abstract class BaseActivity : AppCompatActivity() {
    @Inject
    lateinit var pref: AppSharedPreferences
    @Inject
    lateinit var utils: Utils
    lateinit var mCurrentLanguage: String
    protected lateinit var progressBarUtils: ProgressBarUtils

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        progressBarUtils = ProgressBarUtils(this)
        mCurrentLanguage = "EN"
        pref.setSelectedLanguage(mCurrentLanguage)   
    }
    }

LoginActivity

    class LoginActivity : BaseActivity() {
    private val TAG = this.javaClass.name
    lateinit var loginViewModel: LoginViewModel
    @Inject
    lateinit var viewModelFactory: LoginViewModelFactory


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        AndroidInjection.inject(this)
        loginViewModel = ViewModelProviders.of(this, viewModelFactory).get(
                LoginViewModel::class.java)
        loadData("abc", "xyz")
        observerOnLoginResult()
        observerOnLoginError()
    }


    private fun observerOnLoginError() {
        loginViewModel.loginError().observe(this, Observer<String> {
            progressBarUtils.cancelProgressDialog()
            if (it != null) {
                Toast.makeText(this, resources.getString(R.string.error_message) + it,
                        Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun observerOnLoginResult() {
        loginViewModel.loginResult().observe(this,
                Observer<LoginModel> {
                    progressBarUtils.cancelProgressDialog()
                    if (it != null) {
                        setData(it)
                    }
                })
    }

    private fun setData(loginData: LoginModel) {
        pref.setCurrentUserName(loginData.data.userName)
    }

    private fun loadData(email: String, password: String) {
        progressBarUtils.showProgressDialog()
        val builder = StringBuilder()
        var auth = Base64.encodeToString(("$email:$password").toByteArray(), Base64.NO_WRAP)
        builder.append("Basic ")
        builder.append(auth)
        loginViewModel.getLoginData(builder.toString())
    }

    override fun onDestroy() {
        loginViewModel.disposeElements()
        super.onDestroy()
    }

AppModule

    @Module class AppModule(val app: Application) {

    @Provides
    @Singleton
    fun provideApplication(): Application = app


    @Provides
    @Singleton
    fun provideSharedPreferences(): AppSharedPreferences = AppSharedPreferences(app)


    @Provides
    @Singleton
    fun provideUtils(): Utils = Utils(app)
    }

构建器模块

    @Module abstract class BuildersModule {

    @PerActivity
    @ContributesAndroidInjector(modules = arrayOf(LoginModule::class))
    abstract fun contributeLoginActivity(): LoginActivity

    @PerActivity
    abstract fun contributeBaseActivityActivity(): BaseActivity
    }

AppComponent

    @Singleton @Component(
        modules = arrayOf(AndroidInjectionModule::class, AppModule::class, BuildersModule::class))

    interface AppComponent { 
      fun inject(app: LotusApp)            
    }

推荐答案

在初始化对象之前不能使用它.

You can't use an object before you initialize it.

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState) // prefs called in parents onCreate!
  setContentView(R.layout.activity_login)
  AndroidInjection.inject(this) // injection happens here

按顺序接听电话.修复最简单的方法是将AndroidInjection.inject(this)移到调用super.onCreate(...)的位置.

Get your calls in order. Easiest way to fix would be to move AndroidInjection.inject(this) before the call to super.onCreate(...).

这篇关于UninitializedPropertyAccessException:lateinit属性首选项尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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