I'm a complete newbie to android development and I've been stuck on this problem for the past two days and I've never felt more frustrated in my life. A little backstory first,
I'm creating the most basic Book Library app and I was trying to add a Navigation Drawer to the app. Inside the kotlin file, When I declare all the variables (corresponding to the tags created in the layout file) using lateinit, it throws me a nullPointerException. For this reason I've taken to declaring my variables like this:
var drawerLayout: DrawerLayout? = null
which helps me avoid the exception. Now coming to the real problem,
I was trying to create a click listener for my actionBarDrawerToggle inside the onCreate method this way:
val actionBarDrawerToggle = ActionBarDrawerToggle(this@MainActivity, drawerLayout, R.string.open_drawer, R.string.close_drawer)
drawerLayout.addDrawerListener(actionBarDrawerToggle)
actionBarDrawerToggle.syncState()
and for some reason, the drawerLayout part of the "drawerLayout.addDrawerListener(actionBarDrawerToggle)" line is underlined in red meaning there's an error. When I run it, this is the error it shows me in build window:
Smart cast to 'DrawerLayout!' is impossible, because 'drawerLayout' is a mutable property that could have been changed by this time
I have no ideo how to proceed with this. I've tried a lot of things and none of them is working. I think the error might have something to do with the declaration method I use that I described above. It would be great if someone could help me out
NullPointerException
when usinglateinit
? Maybe you should try fixing your core issue rather than trying to work around it by using avar
. – ianhanniballake