I recently came across an exception while writing a program and it took me a lot of time to debug simply because I was given a wrong exception by the compiler.
Here's my activity code:
private var mCheatMap = HashMap<Int, Boolean>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz)
mCheatMap = savedInstanceState?.getSerializable(KEY_CHEATER) as HashMap<Int, Boolean> ?: HashMap<Int, Boolean>()
}
During runtime, my app crashed and when I looked into the logs, it said ActivityNotFound
exception and in suggestion, it said maybe I didn't declare the activity in my AndroidManifest.xml
Everything seemed to be error free from my side when suddenly I, by chance, made my mCheapMap
variable casting into a safe cast and everything started to work perfectly. e.g.:
private var mCheatMap = HashMap<Int, Boolean>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz)
mCheatMap = savedInstanceState?.getSerializable(KEY_CHEATER) as? HashMap<Int, Boolean> ?: HashMap<Int, Boolean>()
}
Now I have a few confusions:
Why the compiler gave me the
ActivityNotFound
exception while I was having a casting issue?Why using the safe cast operator solved the problem because even without the safe cast operator, my casting was correct?