google-developer-training / android-basics-kotlin-cupcake-app

Apache License 2.0
103 stars 163 forks source link

Android Basics: Navigation backstack #10

Closed adamg92 closed 2 years ago

adamg92 commented 3 years ago

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?

  1. Implement Up button behavior

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?

  1. Run app.
  2. Click on 'One Cupcake' button
  3. Click 'back' on the navigation controller.
  4. Error occurs, "E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.cupcake, PID: 5009 kotlin.UninitializedPropertyAccessException: lateinit property navController has not been initialized at com.example.cupcake.MainActivity.onSupportNavigateUp(MainActivity.kt:44) at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:235) at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109) at androidx.appcompat.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:188) at android.view.View.performClick(View.java:7448) at android.view.View.performClickInternal(View.java:7425) at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28305) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)"

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.

Rayishere commented 3 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.

citrusella commented 3 years ago

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.

citrusella commented 3 years ago

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!

Rayishere commented 3 years ago

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.

android-dev-lxl commented 3 years ago

@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.

tamarabuilds commented 2 years ago

Thank you @citrusella ! I had a val on line 36 that needed to be removed. Much appreciated!