Zxing.net.mobile: Simple Android crashes with unhandled exception when scan invoked - Xamarin Forms

Created on 16 Nov 2017  路  6Comments  路  Source: Redth/ZXing.Net.Mobile

Repro project at https://github.com/jhealy/xammutts/tree/master/ZXingPlaySLN. Screenshot of crash at bottom.

Build out nice simple scan project using samples below. Fails on vs2017 15.3 with several versions of xamarn forms. Unhandled exception deep in android stuff. No source available.

Following instructions here - https://github.com/Redth/ZXing.Net.Mobile

Create basic xamarin forms + pcl
Update nugets
Test run android - fine on emu
Add https://github.com/Redth/ZXing.Net.Mobile and Mobile.Forms to all projects
Build solution = ok

Add following line to android oncreate right after forms.init

    protected override void OnCreate(Bundle bundle)
    {
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;
    base.OnCreate(bundle);
    global::Xamarin.Forms.Forms.Init(this, bundle);
    ZXing.Net.Mobile.Forms.Android.Platform.Init();
    LoadApplication(new App());
    }

Added following to Android->MainActivity.cs

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

Turn on camera and flashlight via built in manifest editor

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.ZXingPlay" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="19" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <application android:label="ZXingPlay.Android"></application>
</manifest>

Wireup MainPage.xaml to a single button that puts the date and time into a label.
Run to HAXM emulator, 7.1 android 25
OK

Wireup mainpage.cs as follows, run it, and get "unhandled exception" somewhere in android after SCAN is hit.

private void Button_Scan_Clicked(object sender, EventArgs e)
{
    // Label_ScanResult.Text = $"{DateTime.UtcNow.ToLocalTime().ToString()}";
    DoScan();
}

private async void DoScan()
{
    var scanner = new ZXing.Mobile.MobileBarcodeScanner();
    var result = await scanner.Scan();

    if (result != null)
    {
        Label_ScanResult.Text = result.Text;
    }
    else
    {
        Label_ScanResult.Text = "no results returned";
    }
}

Moved x.forms down to 2.3.4.270. Same result.
Run on physical phone. Same result.

Second wireup, same blowout

        private async Task DoScan()
        {
            ZXingScannerPage scanPage = new ZXingScannerPage();
            scanPage.OnScanResult += (result) => {
                // Stop scanning
                scanPage.IsScanning = false;

                // Pop the page and show the result
                Device.BeginInvokeOnMainThread(() => {
                    Navigation.PopAsync();
                    Label_ScanResult.Text = result.Text;
                    DisplayAlert("Scanned Barcode", result.Text, "OK");
                });
            };

            await Navigation.PushAsync(scanPage);
        }

zxing-break-mode

Most helpful comment

@jhealy do you mind elaborating on what the problem was and how you fixed it? I am encountering the same problem.

All 6 comments

Main page must be in a NavigationPage. Closing...

@jhealy do you mind elaborating on what the problem was and how you fixed it? I am encountering the same problem.

@jhealy I have the problem, please elaborate how to fix this

Why is this closed I'm getting the same crash result. Here is a small sample app to demostrate teh issue
https://www.syncfusion.com/kb/7662/how-to-capture-and-scan-the-barcode-in-xamarin-forms

This thread fixed my similar issue https://github.com/Redth/ZXing.Net.Mobile/issues/637

Was this page helpful?
0 / 5 - 0 ratings