Search code examples
javascriptandroidangularionic-frameworkcapacitor

App Crashes with FATAL EXCEPTION: java.lang.RuntimeException: Unable to start activity


I have an Ionic, Angular, and Capacitor application. I have recently updated it to the latest version of Ionic 7, Angular 16, and Capacitor5. After the update, on Android when I install it the first time, it works fine without any issues. But When I restart the app then it crashes with the following exception.

FATAL EXCEPTION: main   
Process: App Id, PID: 26600
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{APPID/APPID.MainActivity}: java.lang.NullPointerException: Attempt to 
invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null 
object reference at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3540)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3700)
at 
android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)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:2136) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:236)at android.app.ActivityThread.main(ActivityThread.java:8061) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at com.getcapacitor.Bridge.isNewBinary(Bridge.java:381) at com.getcapacitor.Bridge.loadWebView(Bridge.java:255) 
at com.getcapacitor.Bridge.<init>(Bridge.java:215) 
at com.getcapacitor.Bridge.<init>(Unknown Source:0)
at com.getcapacitor.Bridge$Builder.create(Bridge.java:1513) 
at com.getcapacitor.BridgeActivity.load(BridgeActivity.java:42)
at com.getcapacitor.BridgeActivity.onCreate(BridgeActivity.java:36)
at android.app.Activity.performCreate(Activity.java:8127) 
at android.app.Activity.performCreate(Activity.java:8098)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3513)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3700) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
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:2136) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:236) 
at android.app.ActivityThread.main(ActivityThread.java:8061) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 

Project Info:

Ionic CLI                     : 7.1.1 
Ionic Framework               : @ionic/angular 7.0.14
@angular-devkit/build-angular : 16.1.0
@angular-devkit/schematics    : 16.1.0
@angular/cli                  : 16.1.0
@ionic/angular-toolkit        : 9.0.0

Capacitor:

Capacitor CLI      : 5.1.0
@capacitor/android : 5.1.0
@capacitor/core    : 5.1.0
@capacitor/ios     : 5.1.0
NodeJS : v18.10.0 
npm    : 8.19.2



Android Studio Flamingo | 2022.2.1 Patch 2
Java 17
Gradle 8.0.0

Any Suggestion about what can be the issue and How can I resolve it?


Solution

  • The error you're experiencing is a null pointer on the Android Capacitor side.

    Looking at Bridge.java:381, it seems that either versionCode or versionName are missing.

    private boolean isNewBinary() {
            String versionCode = "";
            String versionName = "";
            SharedPreferences prefs = getContext()
                .getSharedPreferences(com.getcapacitor.plugin.WebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
            String lastVersionCode = prefs.getString(LAST_BINARY_VERSION_CODE, null);
            String lastVersionName = prefs.getString(LAST_BINARY_VERSION_NAME, null);
    
            [...]
    
            // Here it breaks!
            if (!versionCode.equals(lastVersionCode) || !versionName.equals(lastVersionName)) {
       
            [...]
    }
    
    

    Bridge.java

    So to solve this, check if you set versionCode and versionName in your android project settings.

    (File -> Project Structure -> Modules -> Default Config)