NativeScript / nativescript-camera

NativeScript plugin to empower using device camera.
Apache License 2.0
92 stars 47 forks source link

NullPointerException when calling takePicture on application startup #170

Closed ekkiiiii closed 5 years ago

ekkiiiii commented 5 years ago

Hey, I have been having issues getting a camera view to open up open application startup. When i run the application on the device i get the following error: JS: Error: Error: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference JS: android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:544) JS: com.tns.Runtime.callJSMethodNative(Native Method) JS: com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1120) JS: com.tns.Runtime.callJSMethodImpl(Runtime.java:1000) JS: com.tns.Runtime.callJSMethod(Runtime.java:987) JS: com.tns.Runtime.callJSMethod(Runtime.java:967) JS: com.tns.Runtime.callJSMethod(Runtime.java:959) JS: com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:18) JS: android.app.Activity.performCreate(Activity.java:6012) JS: android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) JS: android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) JS: android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) JS: android.app.ActivityThread.access$800(ActivityThread....

Which platform(s) does your issue occur on?

Android 5.1.1 on Zebra TC8000

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

Create a hello world exaple using NativeScript CLI, add permissions for camera in AndroidPermissions.xml (might have to request permissions at runtime in newer android versions) and use the code provided below.

Is there any code involved?

export class AppComponent { constructor() { takePicture() .then((imageAsset: any) => { console.log("picture taken"); }, (error) => { console.log("Error: ", error, error.stack); }); } }

ekkiiiii commented 5 years ago

I have found out that I can work around this issue by adding a (1 second) timeout before opening the camera in the constructor. It then works but the behaviour still differs from what I would expect it to be.

tgpetrov commented 5 years ago

@ekkiiiii You should request permission to access the camera before trying to take picture as shown in the demo application: https://github.com/NativeScript/nativescript-camera/blob/f8c5182455d4479db00a0b647026efabb243968c/demo-angular/app/app.component.ts#L21 Write back if you need further assistance.