cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
537 stars 642 forks source link

activity_recognition_flutter 3.0 NullPointer #309

Closed koenniem closed 3 years ago

koenniem commented 3 years ago

Remember to specify the plugin name in the title!

Device / Emulator and OS

Please complete the following information for each phone and/or emulator you're experiencing this bug on:

Describe the bug

In relation to issue #305, when enabling the foreground service the app crashes with a NullPointerException when trying to read the SharedPreference. In ActivityRecognitionFlutterPlugin.class:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    String result = sharedPreferences.getString("detected_activity", "error");
    Log.d("onSharedPreferenceChang", result);
    if (key.equals("detected_activity")) {
        Log.d("activity_recognition_flutter", "Detected activity: " + result);
        this.eventSink.success(result);
     }
 }

In this code, key is null causing an exception in if (key.equals("detected_activity")) {

This has also been reported earlier in issue #248.

Additional information

Stacktrace:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at dk.cachet.activity_recognition_flutter.ActivityRecognitionFlutterPlugin.onSharedPreferenceChanged(ActivityRecognitionFlutterPlugin.java:163)
        at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:629)
        at android.app.SharedPreferencesImpl$EditorImpl.lambda$notifyListeners$0$SharedPreferencesImpl$EditorImpl(SharedPreferencesImpl.java:643)
        at android.app.-$$Lambda$SharedPreferencesImpl$EditorImpl$3CAjkhzA131V3V-sLfP2uy0FWZ0.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8057)
        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)
koenniem commented 3 years ago

According to the docs, Android devices target API 30 (Android 11) or later receive a null key when preferences are cleared. This does not happen to lower API versions since the listener is not triggered then.

Proposed fix:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    String result = sharedPreferences.getString("detected_activity", "error");
    Log.d("onSharedPreferenceChang", result);
    if (key != null && key.equals("detected_activity")) {
        Log.d("activity_recognition_flutter", "Detected activity: " + result);
        this.eventSink.success(result);
     }
 }
thomasnilsson commented 3 years ago

Thanks, will fix.

thomasnilsson commented 3 years ago

Fixed in version 3.0.1