1

I am using the audio_service package as well as the flutter_background_geolocation package in my application. Both packages are configured for Android as described in their documentation. When clicking the back button of my device, the app closes. Now if I resume too quickly to the App it crashes with following error:

java.lang.RuntimeException: Unable to stop service com.ryanheise.audioservice.AudioService@e4814cd: java.lang.NullPointerException: Attempt to invoke virtual method 'io.flutter.plugin.common.PluginRegistry$RequestPermissionsResultListener com.lyokone.location.FlutterLocationService.getServiceRequestPermissionsResultListener()' on a null object reference
E/TSLocationManager( 9012):     at android.app.ActivityThread.handleStopService(ActivityThread.java:4375)
E/TSLocationManager( 9012):     at android.app.ActivityThread.access$1900(ActivityThread.java:237)
E/TSLocationManager( 9012):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1956)
E/TSLocationManager( 9012):     at android.os.Handler.dispatchMessage(Handler.java:106)
E/TSLocationManager( 9012):     at android.os.Looper.loop(Looper.java:223)
E/TSLocationManager( 9012):     at android.app.ActivityThread.main(ActivityThread.java:7656)
E/TSLocationManager( 9012):     at java.lang.reflect.Method.invoke(Native Method)
E/TSLocationManager( 9012):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/TSLocationManager( 9012):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/TSLocationManager( 9012): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.flutter.plugin.common.PluginRegistry$RequestPermissionsResultListener com.lyokone.location.FlutterLocationService.getServiceRequestPermissionsResultListener()' on a null object reference
E/TSLocationManager( 9012):     at com.lyokone.location.LocationPlugin.dispose(LocationPlugin.java:117)
E/TSLocationManager( 9012):     at com.lyokone.location.LocationPlugin.detachActivity(LocationPlugin.java:56)
E/TSLocationManager( 9012):     at com.lyokone.location.LocationPlugin.onDetachedFromActivity(LocationPlugin.java:69)
E/TSLocationManager( 9012):     at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.detachFromActivity(FlutterEngineConnectionRegistry.java:392)
E/TSLocationManager( 9012):     at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.detachFromAppComponent(FlutterEngineConnectionRegistry.java:297)
E/TSLocationManager( 9012):     at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.destroy(FlutterEngineConnectionRegistry.java:118)
E/TSLocationManager( 9012):     at io.flutter.embedding.engine.FlutterEngine.destroy(FlutterEngine.java:419)
E/TSLocationManager( 9012):     at com.ryanheise.audioservice.AudioServicePlugin.disposeFlutterEngine(AudioServicePlugin.java:103)
E/TSLocationManager( 9012):     at com.ryanheise.audioservice.AudioServicePlugin$AudioHandlerInterface.onDestroy(AudioServicePlugin.java:800)
E/TSLocationManager( 9012):     at com.ryanheise.audioservice.AudioService.onDestroy(AudioService.java:301)
E/TSLocationManager( 9012):     at android.app.ActivityThread.handleStopService(ActivityThread.java:4355)
E/TSLocationManager( 9012):     ... 8 common frames omitted
I/Process ( 9012): Sending signal. PID: 9012 SIG: 9
Lost connection to device.

Her is my AndroidManifest.xml:

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    package="de.geomodo.aschaffenburger_geschichten">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <queries>
        <!-- If your app opens https URLs -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>
    </queries>
    <application
        tools:replace="android:label"
        android:label="Aschaffenburger Geschichten"
        android:icon="@mipmap/ic_launcher">
        <activity
              android:exported="true"
            android:name="com.ryanheise.audioservice.AudioServiceActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
                android:name="io.flutter.embedding.android.NormalTheme"
                android:resource="@style/NormalTheme" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- ADD THIS "SERVICE" element -->
        <service android:name="com.ryanheise.audioservice.AudioService"   android:exported="true"
       >
            <intent-filter>
                <action android:name="android.media.browse.MediaBrowserService" />
            </intent-filter>
        </service>
        <!-- ADD THIS "RECEIVER" element -->
        <receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver"   android:exported="true"
      >
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </receiver>
        <service android:name="com.transistorsoft.locationmanager.service.TrackingService" android:foregroundServiceType="location" />
        <service android:name="com.transistorsoft.locationmanager.service.LocationRequestService" android:foregroundServiceType="location" />
        <!-- flutter_background_geolocation licence -->
        <meta-data android:name="com.transistorsoft.locationmanager.license" android:value="****************************************" />
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

When changing the android:name="com.ryanheise.audioservice.AudioServiceActivity" back to android:name=".MainActivity" the app is not crashing anymore, but of course the audio_service package is not working then.

And this is my main.dart:

    Future main() async {
      WidgetsFlutterBinding.ensureInitialized();
    
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Container(),
          title: 'Aschaffenburger Geschichten',
        );
      }
    }

My flutter doctor output:

[✓] Flutter (Channel stable, 2.8.1, on macOS 12.1 21C52 darwin-arm, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.63.2)
[✓] Connected device (1 available)

Any help?

Martin Seubert
  • 978
  • 3
  • 21
  • 37

0 Answers0