I'm trying to change the app theme during runtime from an options menu item click but it's not working and my app keeps crashing. What's the correct way to reslove this error? Android Studio doesn't provide any suggestions whatsoever, let alone helpful ones.

> 
> java.lang.Boolean cannot be cast to java.lang.Integer
> 
> 
> 

```
class MainActivity : AppCompatActivity() {
    private val prefTheme = "preference_DarkTheme"

    override fun onCreate(savedInstanceState: Bundle?) {
        useDarkTheme = shouldUseDarkTheme()
        setTheme(
            if (useDarkTheme) R.style.DarkTheme
            else R.style.LightTheme
        )

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

    override fun onStart() {
        super.onStart()

        if (useDarkTheme != shouldUseDarkTheme()) recreate()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_changeTheme -> {
                switchTheme()
                restartActivity()

                Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show()
                true
            }
            else -> { super.onOptionsItemSelected(item) }
        }
    }

    private fun getSavedTheme(): Int {
        return PreferenceManager.getDefaultSharedPreferences(this)
            .getInt(prefTheme, R.style.LightTheme)
    }

    private fun switchTheme() {
        val newTheme: Int =
            if (getSavedTheme() == R.style.LightTheme) R.style.DarkTheme else R.style.LightTheme
        PreferenceManager.getDefaultSharedPreferences(this)
            .edit()
            .putInt(prefTheme, newTheme)
            .apply()
    }

    private fun restartActivity() {
        startActivity(
            Intent(this, MainActivity::class.java)
        )
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        finish()
    }

    private fun shouldUseDarkTheme(): Boolean =
        PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
            "preference_DarkTheme",
            false
        )
}

```