YBcodes
YBcodes

Reputation: 19

java.lang.NoSuchMethodError: No virtual method getOnBackInvokedDispatcher()Landroid/window/OnBackInvokedDispatcher;

Facing error:

java.lang.NoSuchMethodError: No virtual method getOnBackInvokedDispatcher()Landroid/window/OnBackInvokedDispatcher; in class Landroid/app/Activity; or its super classes (declaration of 'android.app.Activity' appears in /system/framework/framework.jar)

This is the complete stacktrace

--------- beginning of crash

2023-05-15 22:39:47.627 2707-2707 AndroidRuntime
com....apps.nowinandroid.demo.debug E FATAL EXCEPTION: main Process: com.google.samples.apps.nowinandroid.demo.debug, PID: 2707 java.lang.NoSuchMethodError: No virtual method getOnBackInvokedDispatcher()Landroid/window/OnBackInvokedDispatcher; in class Landroid/app/Activity; or its super classes (declaration of 'android.app.Activity' appears in /system/framework/framework.jar) at androidx.activity.ComponentActivity$Api33Impl.getOnBackInvokedDispatcher(ComponentActivity.java:1138) at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:382) at com.google.samples.apps.nowinandroid.MainActivity.onCreate(MainActivity.kt:78) at android.app.Activity.performCreate(Activity.java:8257) at android.app.Activity.performCreate(Activity.java:8237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1339) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3660) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3816) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:99) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2299) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7850) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

When I tried to run NIA android app on Android studio Flamingo emulator with TIRAMISU API got this error.

I tried to refresh project and reconfigure but did no change in output.

Upvotes: 1

Views: 3153

Answers (3)

Angga Pratama
Angga Pratama

Reputation: 1

I was initially using an emulator with API 31 for my Jetpack Compose project, and the app was functioning correctly. However, when I switched to an emulator running API 33 (Tiramisu), I encountered this issue. Surprisingly, upon testing again with an emulator on API 34, the app worked flawlessly without any code changes.

Upvotes: 0

Piush Kumar
Piush Kumar

Reputation: 159

1> Add the below dependencies in build.gradle file

implementation "androidx.activity:activity-ktx:1.8.0-alpha07"

2> Add the below line in the AndroidManifest.xml file under application tag

android:enableOnBackInvokedCallback="true"

3> Add the below code under on create method

Kotlin :

    val dispatcher: OnBackPressedDispatcher = onBackPressedDispatcher

    // Create a callback for back button handling
    val callback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            // Handle back button press here
            // If you want to allow the default behavior, call super.handleOnBackPressed()
        }
    }

Java :

    OnBackInvokedDispatcher dispatcher = getOnBackInvokedDispatcher();

Upvotes: 4

YBcodes
YBcodes

Reputation: 19

I solved this same error by setting the emulator Android API version to 32. Old version was Tiramisu API.

steps

  1. Goto Device manager
  2. Select emulator device to which the error is happening
  3. Update the API to 32 and save configuration
  4. Run the app on same emulator (It should work fine)

Upvotes: 0

Related Questions