transistorsoft / nativescript-background-geolocation-lt

Sophisticated, battery-conscious, cross-platform background-geolocation with motion-detection and geofencing
MIT License
73 stars 24 forks source link

getting `Unable to destroy activity: Cannot read property 'getIntent' of undefined` #108

Closed Liooo closed 6 years ago

Liooo commented 6 years ago

I installed my app on a device, opened the app which triggers location tracking, leave it for like a day, and the app crashed when I opened the app again. I haven't found the exact way to reproduce it so far, but the error content seems that it's coming from the plugin and that it's possibly enough to identify the reason. Sorry for the lack of the information.

Error content

An uncaught Exception occurred on "main" thread.
java.lang.RuntimeException: Unable to destroy activity {<package name>/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: 
Calling js method onDestroy failed

TypeError: Cannot read property 'getIntent' of undefined
File: "file:///data/data/<package name>/files/app/tns_modules/tns-core-modules/ui/frame/frame.js, line: 663, column: 18

StackTrace: 
    Frame: function:'BackgroundGeolocation.init', file:'file:///data/data/<package name>/files/app/tns_modules/nativescript-background-geolocation-lt/background-geolocation.js', line: 624, column: 48
    Frame: function:'BackgroundGeolocation.getAdapter', file:'file:///data/data/<package name>/files/app/tns_modules/nativescript-background-geolocation-lt/background-geolocation.js', line: 639, column: 18
    Frame: function:'BackgroundGeolocation.onActivityDestroyed', file:'file:///data/data/<package name>/files/app/tns_modules/nativescript-background-geolocation-lt/background-geolocation.js', line: 41, column: 14
    Frame: function:'', file:'file:///data/data/<package name>/files/app/tns_modules/nativescript-background-geolocation-lt/background-geolocation.js', line: 33, column: 27
    Frame: function:'Observable.notify', file:'file:///data/data/<package name>/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 103, column: 23
    Frame: function:'', file:'file:///data/data/<package name>/files/app/tns_modules/tns-core-modules/application/application.js', line: 181, column: 24
    Frame: function:'ActivityCallbacksImplementation.onDestroy', file:'file:///data/data/<package name>/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 663, column: 19
    Frame: function:'NativeScriptActivity.onDestroy', file:'file:///data/data/<package name>/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 36, column: 25

    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4398)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4417)
    at android.app.ActivityThread.-wrap6(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1629)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:199)
    at android.app.ActivityThread.main(ActivityThread.java:6523)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: com.tns.NativeScriptException: 
Calling js method onDestroy failed

TypeError: Cannot read property 'getIntent' of undefined
File: "file:///data/data/<package name>/files/app/tns_modules/tns-core-modules/ui/frame/frame.js, line: 663, column: 18

StackTrace: 
    Frame: function:'BackgroundGeolocation.init', file:'file:///data/data/<package name>/files/app/tns_modules/nativescript-background-geolocation-lt/background-geolocation.js', line: 624, column: 48
    Frame: function:'BackgroundGeolocation.getAdapter', file:'file:///data/data/<package name>/files/app/tns_modules/nativescript-background-geolocation-lt/background-geolocation.js', line: 639, column: 18
    Frame: function:'BackgroundGeolocation.onActivityDestroyed', file:'file:///data/data/<package name>/files/app/tns_modules/nativescript-background-geolocation-lt/background-geolocation.js', line: 41, column: 14
    Frame: function:'', file:'file:///data/data/<package name>/files/app/tns_modules/nativescript-background-geolocation-lt/background-geolocation.js', line: 33, column: 27
    Frame: function:'Observable.notify', file:'file:///data/data/<package name>/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 103, column: 23
    Frame: function:'', file:'file:///data/data/<package name>/files/app/tns_modules/tns-core-modules/application/application.js', line: 181, column: 24
    Frame: function:'ActivityCallbacksImplementation.onDestroy', file:'file:///data/data/<package name>/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 663, column: 19
    Frame: function:'NativeScriptActivity.onDestroy', file:'file:///data/data/<package name>/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 36, column: 25

    at com.tns.Runtime.callJSMethodNative(Native Method)
    at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
    at com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
    at com.tns.Runtime.callJSMethod(Runtime.java:957)
    at com.tns.Runtime.callJSMethod(Runtime.java:941)
    at com.tns.Runtime.callJSMethod(Runtime.java:933)
    at com.tns.NativeScriptActivity.onDestroy(NativeScriptActivity.java:54)
    at android.app.Activity.performDestroy(Activity.java:7136)
    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1158)
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4385)
    ... 9 more

gist of the code

class LocationService {
  constructor() {
    this.startService()
  }

  public startService() {
    const config = {
      debug: false,
      startOnBoot: true,
      stopOnTerminate: false,
      desiredAccuracy: 0,
      stationaryRadius: 10,
      distanceFilter: 0,
      activityRecognitionInterval: 10000,
      autoSync: true,
      // geofencing
      geofenceProximityRadius: 500,
      // iOS
      preventSuspend: false,
      // android
      locationUpdateInterval: 5000,
      foregroundService: true,
      notificationTitle: 'running',
    };

    BackgroundGeolocation.configure(config, ({enabled}) => {
      if (!enabled) {
        BackgroundGeolocation.start(this.onServiceInit.bind(this));
      } else {
        this.onServiceInit();
      }
    });
  }

  private onServiceInit() {
    BackgroundGeolocation.watchPosition((loc)=>{
      console.log("location!");
    })

    BackgroundGeolocation.on('geofence', function(geofence) {
      const identifier = geofence.identifier;
      console.log(`${identifier} - ${geofence.action}`);
    });

    const baseGeoConfig = {radius: 100, notifyOnEntry: true, notifyOnExit: true};
    const coors = [
      {identifier: 'placeA', latitude: ..., longitude: ...},
      {identifier: 'placeB', latitude: ..., longitude: ...},
      {identifier: 'placeC', latitude: ..., longitude: ...},
    ];
    const geofences = coors.map(c => assign(c, baseGeoConfig)) // `assign()` is imported from lodash and equivalent to Object.assign;
    BackgroundGeolocation.addGeofences(
      geofences,
      () => Logger.log("Successfully added geofence"),
      error => Logger.log("Failed to add geofence", error)
    );
  }
}

Your Environment

bradseefeld commented 6 years ago

I am also getting this on nativescript 4.1, tns-android 4.1.2. For me, I can only reproduce when I modify the display size under the android settings. Changing this from largest to default and back (and focusing the app each time) will eventually trigger this. Have not been able to reproduce otherwise.

christocracy commented 6 years ago

I have a significant refactor coming tomorrow built against latest nativescript.

Liooo commented 6 years ago

@christocracy is it done?

christocracy commented 6 years ago

Working on it right now. I'm going to do a final field-test soon with my batch of test-devices.

christocracy commented 6 years ago

1.9.0-beta.3 released. Be sure to do this

Liooo commented 6 years ago

awesome I'll check it, thanks!

bradseefeld commented 6 years ago

FYI, I have not seen this in our production build since upgrading to beta3.

Liooo commented 6 years ago

@christocracy been running the app for a few weeks straight but still working without any problems, thanks 👍

christocracy commented 6 years ago

Glad it's working. Thanks for the update.