Closed adamg92 closed 2 years ago
From the error code above, there is an expectation is raised which exits in the file, MainActivity.kt:44.
kotlin.UninitializedPropertyAccessException: lateinit property navController has not been initialized
I'd like to encourage you to inspect the code in the file, MainActivity.kt:44, and check whether you have the following code private lateinit var navController: NavController
in the MainActivity class as parameters.
The code should be look like this
class MainActivity : AppCompatActivity(R.layout.activity_main){
private lateinit var navController: NavController
...
}
After applying the code from above, you should inspect the code again and make sure there is no red waring in your code. Then the app should be up and running.
Do you have any suggestions for if that part of the code is fine? My code includes that line (in fact, my code appears to be identical to the solution code for the MainActivity.kt), and my code throws an error on the exact same line that OP's does so I'd venture a guess they're having an issue at the same place. No warnings, no visible errors until I run the app and it crashes on my device when hitting the back icon.
Oh. D'oh. Make sure you've removed the var from before the navController line (likely near 36) inside the onCreate function--I overlooked doing that and that was what was tripping the error in my case!
Oh. D'oh. Make sure you've removed the var from before the navController line (likely near 36) inside the onCreate function--I overlooked doing that and that was what was tripping the error in my case!
Yeah, you are right about that! This step is implicit in the instructions from Google Android Developer website.
@adamg92 Thank you for reaching out to us. Please check the solution shared by @citrusella above and if that does not resolve your issue, share the MainActivity.kt kollin file.
Thank you @citrusella ! I had a val on line 36 that needed to be removed. Much appreciated!
URL of codelab https://developer.android.com/codelabs/basic-android-kotlin-training-navigation-backstack?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-unit-3-pathway-4%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-navigation-backstack#2
In which task and step of the codelab can this issue be found?
Describe the problem After importing the source project [android-basics-kotlin-cupcake-app-viewmodel], the instructions indicate to add an override for the 'onSupportNavigateUp' function. Upon implementing the lateinit var navController or overriding the fuction, the app crashes when using the 'back' (or up navigate button).
Steps to reproduce?
Versions _Android Studio version:Android Studio 4.1.3 Build #AI-201.8743.12.41.7199119, built on March 10, 2021 Runtime version: 1.8.0_242-release-1644-b01 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0 GC: ParNew, ConcurrentMarkSweep Memory: 1237M Cores: 8 Registry: ide.new.welcome.screen.force=true, external.system.auto.import.disabled=true Non-Bundled Plugins: org.jetbrains.kotlin
_API version of the emulator: 30
Additional information Include screenshots if they would be useful in clarifying the problem.