Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

activity_recognition_flutter 3.0 NullPointer #309

Closed
koenniem opened this issue Mar 16, 2021 · 3 comments
Closed

activity_recognition_flutter 3.0 NullPointer #309

koenniem opened this issue Mar 16, 2021 · 3 comments
Labels
bugfix a bug fix

Comments

@koenniem
Copy link

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:

  • Device: [e.g. iPhone 6s]
  • OS: [e.g. iOS 13.1]

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 koenniem added the bugfix a bug fix label Mar 16, 2021
@koenniem
Copy link
Author

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);
     }
 }

koenniem added a commit to koenniem/flutter-plugins that referenced this issue Mar 25, 2021
Also suppressed some warnings that show when building. Changed activity tracking to be a broadcast for a JobIntentService.
@thomasnilsson
Copy link
Contributor

Thanks, will fix.

@thomasnilsson
Copy link
Contributor

Fixed in version 3.0.1

SteelSavant pushed a commit to Photorithm/flutter-plugins that referenced this issue Sep 3, 2021
Also suppressed some warnings that show when building. Changed activity tracking to be a broadcast for a JobIntentService.
hibitness-quan-nguyen2 pushed a commit to hibitness/flutter-plugins that referenced this issue Dec 18, 2023
Also suppressed some warnings that show when building. Changed activity tracking to be a broadcast for a JobIntentService.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix a bug fix
Projects
None yet
Development

No branches or pull requests

2 participants