question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Application crashes when asking for permissions [Xamarin.Forms.Android]

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
thedeecommented, May 2, 2019

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.

3reactions
owehbehcommented, Dec 26, 2019

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);
        }
Read more comments on GitHub >

github_iconTop Results From Across the Web

xamarin.android - App Crashing when requesting Location ...
When I move to another tab i just requesting runtime permission for location(ACCESS_FINE_LOCATION), that time application was crashed. private ...
Read more >
Xamarin.Android application crashes only on release
I am developing an application for ios and Android but I have a strange problem for Android: with the same parameters for the...
Read more >
App crash when denied location permission in Xamarin iOS
i need to have location in my app and app got crash when user denied the location permission by clicking "Don't Allow". i...
Read more >
Xamarin Essentials MediaPicker crashing after taking a ...
I'm testing taking pictures with the camera to set a profile pictures on an Android phone. After taking the photo and clicking the...
Read more >
Notification runtime permission
To request the new notification permission from your app, update your app to target Android 13 and complete a similar process compared to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found