Zxing.net.mobile: Application crashes when asking for permissions [Xamarin.Forms.Android]

Created on 30 Apr 2016  路  9Comments  路  Source: Redth/ZXing.Net.Mobile

My application crashes when asking for camera permissions. It only crashe when i use ZXingScannerView instead of ZXingScannerPage.

There's not an issue on iOS with ZXingScannerView. And to be clear there is not an issue when using ZXingScannerPage so i use it in my latest version but still would like to use the ZXingScannerView

I use it inside of a grid in a xaml file

<zxing:ZXingScannerView 
                    IsScanning="{Binding IsScanning}" 
                    IsAnalyzing="{Binding IsAnalyzing}" 
                    IsTorchOn="true" 
                    HasTorch="true"
                    HorizontalOptions="FillAndExpand" 
                    VerticalOptions="FillAndExpand"
                    x:Name="zxing"/>

I've already made everything which is needed to run ZXing on API23 so it is:

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    global::Xamarin.Forms.Forms.Init (this, bundle);
    ZXing.Net.Mobile.Forms.Android.Platform.Init();


    LoadApplication (new App ());
}

public override void OnRequestPermissionsResult (int requestCode, string[] permissions, Permission[] grantResults)
{
    global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult (requestCode, permissions, grantResults);           

    base.OnRequestPermissionsResult (requestCode, permissions, grantResults);
}

and here's the stack trace

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    ... 1 more
Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
  at Xamarin.Forms.Platform.Android.ViewRenderer`2[TView,TNativeView].GetDesiredSize (Int32 widthConstraint, Int32 heightConstraint) [0x00017] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.Platform.Xamarin.Forms.IPlatform.GetNativeSize (Xamarin.Forms.VisualElement view, Double widthConstraint, Double heightConstraint) [0x0008d] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.OnSizeRequest (Double widthConstraint, Double heightConstraint) [0x00033] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.GetSizeRequest (Double widthConstraint, Double heightConstraint) [0x00053] in <filename unknown>:0 
  at Xamarin.Forms.Grid.MeasuredStarredColumns () [0x000b9] in <filename unknown>:0 
  at Xamarin.Forms.Grid.MeasureAndContractStarredColumns (Double width, Double height, Double totalStarsWidth) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Grid.Measure (Double width, Double height, Boolean requestSize) [0x0010e] in <filename unknown>:0 
  at Xamarin.Forms.Grid.OnSizeRequest (Double widthConstraint, Double heightConstraint) [0x0002a] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.GetSizeRequest (Double widthConstraint, Double heightConstraint) [0x00053] in <filename unknown>:0 
  at Xamarin.Forms.Layout.GetSizeRequest (Double widthConstraint, Double heightConstraint) [0x00020] in <filename unknown>:0 
  at Xamarin.Forms.StackLayout.CalculateNaiveLayout (Xamarin.Forms.LayoutInformation layout, StackOrientation orientation, Double x, Double y, Double widthConstraint, Double heightConstraint) [0x000a8] in <filename unknown>:0 
  at Xamarin.Forms.StackLayout.CalculateLayout (Xamarin.Forms.LayoutInformation layout, Double x, Double y, Double widthConstraint, Double heightConstraint, Boolean processExpanders) [0x00058] in <filename unknown>:0 
  at Xamarin.Forms.StackLayout.OnSizeRequest (Double widthConstraint, Double heightConstraint) [0x0002e] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.GetSizeRequest (Double widthConstraint, Double heightConstraint) [0x00053] in <filename unknown>:0 
  at Xamarin.Forms.Layout.GetSizeRequest (Double widthConstraint, Double heightConstraint) [0x00020] in <filename unknown>:0 
  at Xamarin.Forms.StackLayout.CalculateNaiveLayout (Xamarin.Forms.LayoutInformation layout, StackOrientation orientation, Double x, Double y, Double widthConstraint, Double heightConstraint) [0x000a8] in <filename unknown>:0 
  at Xamarin.Forms.StackLayout.CalculateLayout (Xamarin.Forms.LayoutInformation layout, Double x, Double y, Double widthConstraint, Double heightConstraint, Boolean processExpanders) [0x00058] in <filename unknown>:0 
  at Xamarin.Forms.StackLayout.LayoutChildren (Double x, Double y, Double width, Double height) [0x0005b] in <filename unknown>:0 
  at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x000c7] in <filename unknown>:0 
  at Xamarin.Forms.Layout.OnSizeAllocated (Double width, Double height) [0x0000f] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.SizeAllocated (Double width, Double height) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.SetSize (Double width, Double height) [0x00021] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.set_Bounds (Rectangle value) [0x0006c] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.Layout (Rectangle bounds) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (Xamarin.Forms.VisualElement child, Rectangle region) [0x00113] in <filename unknown>:0 
  at Xamarin.Forms.Page.LayoutChildren (Double x, Double y, Double width, Double height) [0x00107] in <filename unknown>:0 
  at Xamarin.Forms.Page.UpdateChildrenLayout () [0x000c6] in <filename unknown>:0 
  at Xamarin.Forms.Page.OnSizeAllocated (Double width, Double height) [0x0000f] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.SizeAllocated (Double width, Double height) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.SetSize (Double width, Double height) [0x00021] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.set_Bounds (Rectangle value) [0x0006c] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.Layout (Rectangle bounds) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (Xamarin.Forms.VisualElement child, Rectangle region) [0x00113] in <filename unknown>:0 
  at Xamarin.Forms.Page.LayoutChildren (Double x, Double y, Double width, Double height) [0x00107] in <filename unknown>:0 
  at Xamarin.Forms.Page.UpdateChildrenLayout () [0x000c6] in <filename unknown>:0 
  at Xamarin.Forms.Page.OnSizeAllocated (Double width, Double height) [0x0000f] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.SizeAllocated (Double width, Double height) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Page.OnChildMeasureInvalidated (Xamarin.Forms.VisualElement child, InvalidationTrigger trigger) [0x000b6] in <filename unknown>:0 
  at Xamarin.Forms.Page.OnChildMeasureInvalidated (System.Object sender, System.EventArgs e) [0x00013] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.InvalidateMeasure (InvalidationTrigger trigger) [0x0001d] in <filename unknown>:0 
  at Xamarin.Forms.VisualElement.set_IsPlatformEnabled (Boolean value) [0x0001c] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.Platform+<>c.<.cctor>b__105_0 (Xamarin.Forms.BindableObject bindable, System.Object oldvalue, System.Object newvalue) [0x0000a] in <filename unknown>:0 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindablePropertyContext context, System.Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent) [0x0010e] in <filename unknown>:0 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) [0x0014b] in <filename unknown>:0 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, Boolean fromStyle, Boolean checkAccess) [0x0005f] in <filename unknown>:0 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.Platform.SetRenderer (Xamarin.Forms.VisualElement bindable, IVisualElementRenderer value) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.NavigationRenderer.SwitchContentAsync (Xamarin.Forms.Page view, Boolean animated, Boolean removed) [0x0002f] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.NavigationRenderer.OnPushAsync (Xamarin.Forms.Page view, Boolean animated) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.NavigationRenderer.PushViewAsync (Xamarin.Forms.Page page, Boolean animated) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.NavigationRenderer.OnPushed (System.Object sender, Xamarin.Forms.NavigationRequestedEventArgs e) [0x0000e] in <filename unknown>:0 
  at Xamarin.Forms.NavigationPage+<PushAsyncInner>d__66.MoveNext () [0x00065] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <filename unknown>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0004e] in <filename unknown>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in <filename unknown>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in <filename unknown>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.NavigationPage+<PushAsync>d__65.MoveNext () [0x001c4] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <filename unknown>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0004e] in <filename unknown>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in <filename unknown>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in <filename unknown>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <filename unknown>:0 
  at SmykMobile.LoginPageViewModel+<LoginPageViewModel>c__async1.MoveNext () [0x00073] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <filename unknown>:0 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) [0x00000] in <filename unknown>:0 
  at Android.App.SyncContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in <filename unknown>:0 
  at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in <filename unknown>:0 
  at Java.Lang.IRunnableInvoker.n_Run (IntPtr jnienv, IntPtr native__this) [0x00009] in <filename unknown>:0 
  at (wrapper dynamic-method) System.Object:450d8863-91c6-4dc2-8ea7-1900200dff74 (intptr,intptr)
    at mono.java.lang.RunnableImplementor.n_run(Native Method)
    at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    ... 3 more
bug

Most helpful comment

Is there a real solution to this yet? When I choose DENY the application crashes:
System.UnauthorizedAccessException: ZXing.Net.Mobile requires: android.permission.CAMERA, but was not found in your AndroidManifest.xml file.

@thedee even im facing this issue..can you help me if you have fixed this this issue ?
@thedee

So I came across the same problem and I was able to find a temporary workaround until this issue is solved.

In order to avoid the error I iterated the permissions in the OnRequestPermissionsResult and checked if the permission is CAMERA and the result is granted, let ZXing handle OnRequestPermissionsResult.

for (int i = 0; i < permissions.Length; i++)
            {
                //var permission = permissions[i];
                //var grantResult = grantResults[i];
                if (permissions[i].Equals("android.permission.CAMERA") && grantResults[i] == Permission.Granted)
                    global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }

So the whole OnRequestPermissionsResult is now like

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            for (int i = 0; i < permissions.Length; i++)
            {
                if (permissions[i].Equals("android.permission.CAMERA") && grantResults[i] == Permission.Granted)
                    global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

All 9 comments

Ok i got another problem which I've solved by recompiling sources of ZXing.Mobile.Android

When i deny acces to camera it throws and exception like "ZXing requires permission android ... camera"

I just commented out line with the exception thrown like below:

The case is - why does it even throw exception which i can't handle from within Xamarin.Forms ?? Has to be fixed.

bool CheckPermissions (string permission, bool throwOnError = true)
        {
            var result = true;
            var perf = PerformanceCounter.Start ();

            Android.Util.Log.Debug (MobileBarcodeScanner.TAG, "Checking " + permission + "...");

            if (!PlatformChecks.IsPermissionInManifest (this.Context, permission)
                || !PlatformChecks.IsPermissionGranted (this.Context, permission)) {

                result = false;

                if (throwOnError) {
                    var msg = "ZXing.Net.Mobile requires: " + permission + ", but was not found in your AndroidManifest.xml file.";
                    Android.Util.Log.Error ("ZXing.Net.Mobile", msg);

                    //throw new UnauthorizedAccessException (msg);
                }
            }

            PerformanceCounter.Stop (perf, "CheckPermissions took {0}ms");

            return result;
        }

This is fixed in master.

@Redth this issue has reopened in version 2.3.2

Code for permission handler.
global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);

Possible (inaccurate) solution is replace in module CameraController.cs string
ZXing.Net.Mobile.Android.PermissionsHandler.CheckCameraPermissions(_context);
to

    if (!ZXing.Net.Mobile.Android.PermissionsHandler.CheckCameraPermissions(_context, false))
            {
                ZxingActivity.RequestCancel();
                return;
            }

Is there a real solution to this yet? When I choose DENY the application crashes:
System.UnauthorizedAccessException: ZXing.Net.Mobile requires: android.permission.CAMERA, but was not found in your AndroidManifest.xml file.

Is there a real solution to this yet? When I choose DENY the application crashes:
System.UnauthorizedAccessException: ZXing.Net.Mobile requires: android.permission.CAMERA, but was not found in your AndroidManifest.xml file.

@thedee even im facing this issue..can you help me if you have fixed this this issue ?

Is there a real solution to this yet? When I choose DENY the application crashes:
System.UnauthorizedAccessException: ZXing.Net.Mobile requires: android.permission.CAMERA, but was not found in your AndroidManifest.xml file.

@thedee even im facing this issue..can you help me if you have fixed this this issue ?
@thedee

So I came across the same problem and I was able to find a temporary workaround until this issue is solved.

In order to avoid the error I iterated the permissions in the OnRequestPermissionsResult and checked if the permission is CAMERA and the result is granted, let ZXing handle OnRequestPermissionsResult.

for (int i = 0; i < permissions.Length; i++)
            {
                //var permission = permissions[i];
                //var grantResult = grantResults[i];
                if (permissions[i].Equals("android.permission.CAMERA") && grantResults[i] == Permission.Granted)
                    global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }

So the whole OnRequestPermissionsResult is now like

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            for (int i = 0; i < permissions.Length; i++)
            {
                if (permissions[i].Equals("android.permission.CAMERA") && grantResults[i] == Permission.Granted)
                    global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

Will this ever be fixed in the public nuget release?

Was this page helpful?
0 / 5 - 0 ratings