Я хочу вызвать 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)
}
}
Activity_setting
<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>