UninitializedPropertyAccessException:lateinit属性settingActivity尚未初始化

时间:2020-05-04 15:32:59

标签: android kotlin android-dark-theme

我想将Function调用到MainActivity中,但是会出现这样的Error

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property settingActivity has not been initialized
         at com.hafidsuhanizar.infofilm.MainActivity.onCreate (MainActivity.kt: 38)

MainActivity.kt

class MainActivity : AppCompatActivity(){


private var eyp: Switch? = null
internal lateinit var settingActivity: SettingActivity
internal lateinit var sharedpref: SharedPref

override fun onCreate(savedInstanceState: Bundle?) {
    sharedpref = SharedPref(this)
    if (sharedpref.loadNightModeState() == true){
        setTheme(R.style.DarkTheme)
    }else{
        setTheme(R.style.AppTheme)
    }


    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    eyp = settingActivity.findViewById<View>(R.id.enableDark) as Switch?  ----> EROR CODE <---
    if (sharedpref.loadNightModeState() == true){
        eyp!!.isChecked = true
    }

    eyp!!.setOnCheckedChangeListener { buttonView, isChecked ->
        if (isChecked) {
            sharedpref.setNightModeState(true)
            restartApp()
        } else {
            sharedpref.setNightModeState(false)
            restartApp()
        }
    }

    val navView: BottomNavigationView = findViewById(R.id.nav_view)

    val navController = findNavController(R.id.nav_host_fragment)
    val appBarConfiguration = AppBarConfiguration(setOf(
            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)


}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    val inflater: MenuInflater = menuInflater
    inflater.inflate(R.menu.menu, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
    when(item?.itemId) {
        R.id.action_settings -> {
            val intent = Intent(this, SettingActivity::class.java)
            startActivity(intent)
            return true
        }
    }

    return false
}

fun restartApp(){
    val i = Intent(applicationContext,MainActivity::class.java)
    startActivity(i)
    finish()
}

SettingActivity.kt

class SettingActivity : AppCompatActivity() {

private var eyp: Switch? = null
internal lateinit var sharedpref: SharedPref

override fun onCreate(savedInstanceState: Bundle?) {
    sharedpref = SharedPref(this)
    if (sharedpref.loadNightModeState() == true){
        setTheme(R.style.DarkTheme)
    }else{
        setTheme(R.style.AppTheme)
    }

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_setting)

    eyp = findViewById<View>(R.id.enableDark) as Switch?
    if (sharedpref.loadNightModeState() == true){
        eyp!!.isChecked = true
    }

    eyp!!.setOnCheckedChangeListener { buttonView, isChecked ->
        if (isChecked) {
            sharedpref.setNightModeState(true)
            restartApp()
        } else {
            sharedpref.setNightModeState(false)
            restartApp()
        }
    }
}
fun restartApp(){
    val i = Intent(applicationContext,SettingActivity::class.java)
    startActivity(i)
    finish()
}

}

SharefPref.kt

class SharedPref(context: Context){

internal var mySharedPref:SharedPreferences

init {
    mySharedPref = context.getSharedPreferences("filename",Context.MODE_PRIVATE)
}

fun setNightModeState(state : Boolean?){
    val editor = mySharedPref.edit()
    editor.putBoolean("Night Mode",state!!)
    editor.commit()
}

fun loadNightModeState(): Boolean?{
    return mySharedPref.getBoolean("Night Mode",false)
}

}

活动设置

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        app:cardBackgroundColor="?attr/cardbackgroundcolor">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="10dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/enabled_dark_mode"
                android:textColor="?attr/textcolor"
                android:textStyle="bold" />

            <Switch
                android:id="@+id/enableDark"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="@string/night_mode"
                android:textColor="?attr/textcolor"/>

        </LinearLayout>
    </androidx.cardview.widget.CardView>

1 个答案:

答案 0 :(得分:0)

在您的 MainActivity 中,您已经初始化了sharedpred = SharedPref(this)之类的sharedpref变量,就像您必须对其进行初始化一样,它是settingActivity变量,因此,错误将自行解决。