UninitializedPropertyAccessException:lateinit属性pref尚未初始化

时间:2018-05-29 07:24:39

标签: android kotlin dagger-2

我知道这个问题已经回答[这里](lateinit property has not been initialized)。但这是由于黄油刀库问题但我的情况有所不同。完全不同。就我而言 当我在我的基本活动中使用匕首注入属性时,它显示错误引起:kotlin.UninitializedPropertyAccessException:lateinit属性pref尚未初始化

但是当我在我的子活动(登录活动)中使用它时,它的工作正常。

例如。 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)            
}

1 个答案:

答案 0 :(得分:16)

在初始化之前,不能使用对象。

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(...)