Android app crash when launching another app in UART service

28 Views Asked by At

My goal is to launch another app (e.g. WhatsApp) when the phone receives a message from a connected Bluetooth device (e.g. app_ID = 1) in UART service. However, I cannot make it work and my app crashes when trying to launch another app. I am an Android beginner, so please help me. I highly appreciate your suggestions.

I tested to launch the app using a "OpenApp" button, and it works fine. In MainActivity.java, the openApp function is called when I press the "Open App" button.

    public void openApp(View view) {
        Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
        if (launchIntent != null) {
            startActivity(launchIntent);
        } else {
            Toast.makeText(MainActivity.this, "There is no package", Toast.LENGTH_LONG).show();
        }

However, when I tried to launch the same app by calling the function launchApp fromthe UARTService.java, my app crashes:

In MainActivity.java:

    public void launchApp() {
        Log.i(SON_TAG,"launchApp");
        Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
        if (launchIntent != null) {
            startActivity(launchIntent);
        } else {
            Toast.makeText(MainActivity.this, "There is no package", Toast.LENGTH_LONG).show();
        }
    }

In UARTService.java:

    private void open_apps( byte app_ID){
            Log.i(SON_TAG,"App is opening: "+ app_ID);
            MainActivity mActivity = new MainActivity();
            mActivity.launchApp();
    }

Here is the logcat:

2024-02-06 22:23:52.671 30299-30299 SON_DEBUGGING_TAG       com.welie.btserver                   I  launchApp
2024-02-06 22:23:52.671 30299-30299 AndroidRuntime          com.welie.btserver                   D  Shutting down VM
2024-02-06 22:23:52.677 30299-30299 AndroidRuntime          com.welie.btserver                   E  FATAL EXCEPTION: main
                                                                                                    Process: com.welie.btserver, PID: 30299
                                                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
                                                                                                        at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:109)
                                                                                                        at com.welie.btserver.MainActivity.launchApp(MainActivity.java:172)
                                                                                                        at com.welie.btserver.UARTService.open_apps(UARTService.java:134)
                                                                                                        at com.welie.btserver.UARTService.main_control(UARTService.java:124)
                                                                                                        at com.welie.btserver.UARTService.onCharacteristicWrite(UARTService.java:87)
                                                                                                        at com.welie.btserver.BluetoothServer$1.onCharacteristicWrite(BluetoothServer.java:68)
                                                                                                        at com.welie.blessed.BluetoothPeripheralManager$1$5.run(BluetoothPeripheralManager.java:209)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:938)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:226)
                                                                                                        at android.os.Looper.loop(Looper.java:313)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8663)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
2024-02-06 22:23:52.728 30299-30299 Process                 com.welie.btserver                   I  Sending signal. PID: 30299 SIG: 9

Is there any way to launch the app from the UARTService.java directly? I tried to use "getPackageManager().getLaunchIntentForPackage("com.whatsapp")" in UARTService.java, but it does not work.

0

There are 0 best solutions below