title: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager
body: <p><strong>Introduction</strong></p>
  <p>I am running flutter v1.17.5 DeviceApps v1.0.10 and SystemAlertWindow v0.2.2+3 (intentially not the latest version). And I want to open my app from a system alert window that is running in the foreground, even when the app is closed.</p>
  <p>I am using the <strong>SystemOverlayWindow</strong> plugin and the plugin is a activity <a href="https://github.com/pvsvamsi/SystemAlertWindow/blob/master/android/src/main/java/in/jvapps/system_alert_window/SystemAlertWindowPlugin.java" rel="nofollow noreferrer">SystemAlertWindowPlugin.java</a></p>
  <p>In my <strong>Application.kt</strong> I register the plugins and pass the registry</p>
  <pre><code>public class Application: FlutterApplication(), PluginRegistrantCallback {
  
     override fun onCreate() {
       super.onCreate();
       FlutterFirebaseMessagingService.setPluginRegistrant(this);
       SystemAlertWindowPlugin.setPluginRegistrant(this);
       createNotificationChannels();
       FlutterMain.startInitialization(this);
     }
  
     override fun registerWith(registry: PluginRegistry?) {
      if (!registry!!.hasPlugin(&quot;io.flutter.plugins.firebasemessaging&quot;)) {
        FirebaseMessagingPlugin.registerWith(registry!!.registrarFor(&quot;io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin&quot;));
      }
      if (!registry!!.hasPlugin(&quot;in.jvapps.system_alert_window&quot;)) {
        SystemAlertWindowPlugin.registerWith(registry!!.registrarFor(&quot;in.jvapps.system_alert_window&quot;));
      }
      if (!registry!!.hasPlugin(&quot;fr.g123k.deviceapps&quot;)) {
        DeviceAppsPlugin.registerWith(registry!!.registrarFor(&quot;fr.g123k.deviceapps&quot;));
      }
     }
  </code></pre>
  <p>I also register another plugin called <strong>DeviceApps</strong>. This is the DeviceAppsPlugin <a href="https://github.com/g123k/flutter_plugin_device_apps/blob/master/android/src/main/java/fr/g123k/deviceapps/DeviceAppsPlugin.java" rel="nofollow noreferrer">DeviceAppsPlugin.java</a></p>
  <p><strong>Problem</strong></p>
  <p><strong>In short</strong></p>
  <p>So the system overlay (which runs in the foreground) calls &gt; the dart callback &gt; invokes a method of the DeviceApps plugin &gt; error occcurs</p>
  <p><strong>Long version</strong></p>
  <p>I have a static callback that is registered <a href="https://github.com/pvsvamsi/SystemAlertWindow/blob/e0b4cd307a08c8b90b0bc1c38ef1c854e374088d/android/src/main/java/in/jvapps/system_alert_window/SystemAlertWindowPlugin.java#L168" rel="nofollow noreferrer">here</a> and it gets called when I have a click interaction with the system alert window. But now I wan't to call the DeviceApps plugin in my dart code from the that static callback</p>
  <p>So the method channel will invoke <a href="https://github.com/pvsvamsi/SystemAlertWindow/blob/e0b4cd307a08c8b90b0bc1c38ef1c854e374088d/android/src/main/java/in/jvapps/system_alert_window/SystemAlertWindowPlugin.java#L243" rel="nofollow noreferrer">this</a> and that will run the static callback defined in dart.</p>
  <p>This is the static dart callback that is registered and called with a background channel</p>
  <pre><code>  static Future&lt;void&gt; systemOverlayOnClickListner(String tag) async {
      switch (tag) {
        case 'button_app_to_foreground':
          DeviceApps.openApp('com.companyname.appname'); // this is where I try to run the plugin
          await SystemAlertWindow.closeSystemWindow();
          break;
      }
  </code></pre>
  <p>The callback will invoke a method of the DeviceApps plugin. And this causes problems because <a href="https://github.com/g123k/flutter_plugin_device_apps/blob/20d8a64cb5920b0c0a9d067f38b1fc9dc3bafacc/android/src/main/java/fr/g123k/deviceapps/DeviceAppsPlugin.java#L156" rel="nofollow noreferrer">this</a> method will try to get the package manager from the activity passed in it's constructor. But according to this error the activity is null.</p>
  <pre><code>E/MethodChannel#g123k/device_apps(24210): Failed to handle method call
  E/MethodChannel#g123k/device_apps(24210): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.app.Activity.getPackageManager()' on a null object reference
  E/MethodChannel#g123k/device_apps(24210):   at fr.g123k.deviceapps.DeviceAppsPlugin.openApp(DeviceAppsPlugin.java:141)
  </code></pre>
  <p>So it will invoke the getPackageManager() on a null object.</p>
  <p>The activity is only null when it is being called from this static callback that is called by the background channel. But not when I call it normally from the app scope. Why is this the case?</p>
  <p><strong>Conslusion</strong></p>
  <p>So in conclusion calling the plugin works fine when I call it from my app scope. But once the callback is called trough means of the background channel all of the sudden the activity is null.</p>
  <p>I cannot just start a isolate in my app and send a message to that from my callback like how its done <a href="https://pub.flutter-io.cn/packages/system_alert_window#isolate-communication" rel="nofollow noreferrer">here</a>. Because I need this code to work when the app is closed, and a app scoped isolate doesn't run in the background.</p>
  <p>So how can I open my app from the callback?</p>
  <p>This is the full stack trace</p>
  <pre><code>E/flutter (26735): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.content.pm.PackageManager android.app.Activity.getPackageManager()' on a null object reference, null)
  E/flutter (26735): #0      StandardMethodCodec.decodeEnvelope 
  package:flutter/…/services/message_codecs.dart:569
  E/flutter (26735): #1      MethodChannel._invokeMethod 
  package:flutter/…/services/platform_channel.dart:156
  E/flutter (26735): &lt;asynchronous suspension&gt;
  E/flutter (26735): #2      MethodChannel.invokeMethod 
  package:flutter/…/services/platform_channel.dart:329
  E/flutter (26735): #3      DeviceApps.openApp 
  package:device_apps/device_apps.dart:81
  E/flutter (26735): #4      SystemOverlayController.systemOverlayOnClickListner 
  package:appname/…/singletons/system_overlay_controller.dart:51
  E/flutter (26735): #5      callbackDispatcher.&lt;anonymous closure&gt; 
  package:system_alert_window/system_alert_window.dart:136
  E/flutter (26735): #6      MethodChannel._handleAsMethodCall 
  package:flutter/…/services/platform_channel.dart:409
  E/flutter (26735): #7      MethodChannel.setMethodCallHandler.&lt;anonymous closure&gt; 
  package:flutter/…/services/platform_channel.dart:377
  E/flutter (26735): #8      _DefaultBinaryMessenger.handlePlatformMessage 
  package:flutter/…/services/binding.dart:199
  E/flutter (26735): #9      _invoke3.&lt;anonymous closure&gt;  (dart:ui/hooks.dart:290:15)
  E/flutter (26735): #10     _rootRun  (dart:async/zone.dart:1184:13)
  E/flutter (26735): #11     _CustomZone.run  (dart:async/zone.dart:1077:19)
  E/flutter (26735): #12     _CustomZone.runGuarded  (dart:async/zone.dart:979:7)
  E/flutter (26735): #13     _invoke3  (dart:ui/hooks.dart:289:10)
  E/flutter (26735): #14     _dispatchPlatformMessage  (dart:ui/hooks.dart:164:5
  </code></pre>
  <p><strong>Example Repo</strong></p>
  <p>I even tried to add the open app method directly to a <a href="https://github.com/michael-ottink/system_alert_window/blob/578aa59a4356d4f5a85f8022130ed6606a281bef/android/src/main/java/in/jvapps/system_alert_window/SystemAlertWindowPlugin.java#L358" rel="nofollow noreferrer">forked</a> version of the system alert window. And implemented it into a example repo that you can find here, take the branch called my-branch.</p>
  <p><a href="https://github.com/michael-ottink/system_overlay_callback_null_activity" rel="nofollow noreferrer">https://github.com/michael-ottink/system_overlay_callback_null_activity</a></p>
  <p>But it throws the exact same error. Even if I use exactly the same activity. So it has something to do with the background channel I think.</p>
  <p>Run the app and then click the button to get the overlay. Bring the app to the background and click on open in the overlay. The error occurs.</p>
  <p><strong>Extra info</strong></p>
  <p>I think <a href="https://stackoverflow.com/a/58813854/4214922">this</a> is a similar issue only here they choose to not register the plugin because it is foreground only. Where in my case I want to fork either of these plugins and modify the them so that it also works in the background. How do I do that?</p>