Closed ekingunoncu closed 5 years ago
@ekingunoncu do you have an example of your code. I'm running into similar problems.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
Hi! @codytooker I am so sorry I cant give you an example of code because I lost them. I changed my development environment. But I can tell you what I experienced.
Firstly, the reason of the NullPointerException might be the version difference of nativescript. In node modules under nativescript-health-data directory there are the modules javascript files both for ios and android.
Basically what I did for solve the problem was changing all "application.android.currentContext" to "application.android.context". Because latest nativescript version (6). "application.android.currentContext" returned null.
But I don't recommend changing something in node_modules. Changes can be easily forgotten. And If you npm install they would be overridden by the orginal codes.
The proper approach would be moving the nativescript-health-data directory to in our project under app, some place we like.
But it wont work if we don't add google fit and google services dependencies to "App_Resources/Android/app.gradle" file.
Here are dependencies https://developers.google.com/android/guides/setup
After I solved all the problems with google fit api connections. I realized that using google fit api is pain in the ass. It is so stupid to work that much hard to get some data from the device itself.
It is complicated to use, thats why I decided to use device sensors to count steps But at that time handling background processes with nativescript is another pain. As a result now I am writing native Java. Nativescript is just great but I realized for this project it took much more time.
application.android.currentContext
has been removed in NativeScript 6
Use:
(application.android.foregroundActivity || application.android.startActivity)
or
application.android.context
instead.
I created a Pull Request to fix this error today: https://github.com/EddyVerbruggen/nativescript-health-data/pull/30
i faced following error. How to solve it? ERROR Error: Uncaught (in promise): Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference com.google.android.gms.auth.api.signin.internal.zzo.zzbp(Unknown Source:3) com.google.android.gms.auth.api.signin.GoogleSignIn.getLastSignedInAccount(Unknown Source:0) com.tns.Runtime.callJSMethodNative(Native Method) com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1209) com.tns.Runtime.callJSMethodImpl(Runtime.java:1096) com.tns.Runtime.callJSMethod(Runtime.java:1083) com.tns.Runtime.callJSMethod(Runtime.java:1063) com.tns.Runtime.callJSMethod(Runtime.java:1055) com.tns.gen.java.lang.Object_vendor_112044_32_ClickListenerImpl.onClick(Object_vendor_112044_32_ClickListenerImpl.java:18) android.view.View.performClick(View.java:6597) android.view.View.performClickInternal(View.java:6574) android.view.View.access$3100(View.java:778) android.view.View$PerformClick.run(View.java:25885) android.os.Handler.handleCallback(Handler.java:873) android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:193) android.app.ActivityThread.main(ActivityThread.java:6669) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference com.google.android.gms.auth.api.signin.internal.zzo.zzbp(Unknown Source:3) com.google.android.gms.auth.api.signin.GoogleSignIn.getLastSignedInAccount(Unknown Source:0) com.tns.Runtime.callJSMethodNative(Native Method) com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1209) com.tns.Runtime.callJSMethodImpl(Runtime.java:1096) com.tns.Runtime.callJSMethod(Runtime.java:1083) com.tns.Runtime.callJSMethod(Runtime.java:1063) com.tns.Runtime.callJSMethod(Runtime.java:1055) com.tns.gen.java.lang.Object_vendor_112044_32_ClickListenerImpl.onClick(Object_vendor_112044_32_ClickListenerImpl.java:18) android.view.View.performClick(View.java:6597) android.view.View.performClickInternal(View.java:6574) android.view.View.access$3100(View.java:778) android.view.View$PerformClick.run(View.java:25885) android.os.Handler.handleCallback(Handler.java:873) android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:193) android.app.ActivityThread.main(ActivityThread.java:6669) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) at file:///data/data/org.nativescript.healthdata/files/app/vendor.js:82835:62 at new ZoneAwarePromise (file:///data/data/org.nativescript.healthdata/files/app/vendor.js:81700:29) at HealthData.push.../node_modules/nativescript-health-data/health-data.js.HealthData.isAuthorized (file:///data/data/org.nativescript.healthdata/files/app/vendor.js:82829:16) at HomeComponent.module.exports.push../app/home/home.component.ts.HomeComponent.isAuthorized (file:///data/data/org.nativescript.healthdata/files/app/bundle.js:222:25) at Object.eval [as handleEvent] (ng:///AppModule/HomeComponent.ngfactory.js:28:27) at handleEvent (file:///data/data/org.nativescript.healthdata/files/app/vendor.js:65186:41) at callWithDebugContext (file:///data/data/org.nativescript.healthdata/files/app/vendor.js:66256:25) at Object.debugHandleEvent [as handleEvent] (file:///data/data/org.nativescript.healthdata/files/app/vendor.js:65983:12) at dispatchEvent (file:///data/data/org.nativescript.healthdata/files/app/vendor.js:55806:25) at file:///data/data/org.nativescript.healthdata/files/app/vendor.js:64395:38 at file:///data/data/org.nativescript.healthdata/files/app/vendor.js:78029:26 at ZoneDelegate.push.../node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js.ZoneDelegate.invoke (file:///data/data/org.nativescript.healthdata/files/app/vendor.js:81186:26) at Object.onInvoke (file:///data/data/org.nativescript.healthdata/files/app/vendor.js:62203:33) at ZoneDelegate.push.../node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js.ZoneDelegate.invoke (file:///data/data/org.nativescript.healthdata/files/app/vendor.js:81185:32) at Zone.push.../node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js.Zone.run (file:///data/data/org.nativescript.healthdata/files/app/vendor.js:80936:43)
Fixed by #30
application.android.currentContext is undefined.
var application = require("tns-core-modules/application");
There might be a change with the object application. application.android.currentContext is undefined while application.android.context defined and gives context.
Getting the context with the way below also solved the issue for me.
var utilsAd = require("utils/utils").ad; utilsAd.getApplicationContext();