-1

我想调用FunctionintoMainActivityError发生这样的情况:

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

设置活动.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>
4

1 回答 1

0

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

于 2020-05-04T15:43:19.813 回答