Zxing.net.mobile: Android NullReference at GetContext

Created on 27 Oct 2017  路  4Comments  路  Source: Redth/ZXing.Net.Mobile

UNHANDLED EXCEPTION:
System.NullReferenceException: Object reference not set to an instance of an object.
at ZXing.Mobile.MobileBarcodeScanner.GetContext (Android.Content.Context context)
at ZXing.Mobile.MobileBarcodeScanner.Scan (Android.Content.Context context, ZXing.Mobile.MobileBarcodeScanningOptions options)
at ZXing.Mobile.MobileBarcodeScanner.Scan (ZXing.Mobile.MobileBarcodeScanningOptions options)
at ZXing.Mobile.MobileBarcodeScannerBase.Scan ()
at App4.MainPage+d__1.MoveNext ()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state)
at Android.App.SyncContext+<>c__DisplayClass2_0.b__0 ()
at Java.Lang.Thread+RunnableImplementor.Run ()
at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this)
at (wrapper dynamic-method) System.Object:7b87ce6b-d56f-4d47-a91e-06914b52665c (intptr,intptr)

Reproduction steps:

  1. Create new blank PCL app, Xamarin.Forms v2.4.0.280
  2. Install ZXing.Net.Mobile.Forms v2.3.2
  3. Add a button with Clicked handler ( basically sample code from readme):
public async void OnButtonClicked(object sender, EventArgs args)
{
    #if __ANDROID__ 
        // Initialize the scanner first so it can track the current context
        MobileBarcodeScanner.Initialize (Application);
    #endif

    var scanner = new ZXing.Mobile.MobileBarcodeScanner();
    var result = await scanner.Scan();
    if (result != null)
    {
        await DisplayAlert("Result", result.Text, "OK");
    }
}

When it comes to platform-specific initialization steps the only difference is that I didn't add the flashlight permission.

Most helpful comment

Not sure if this is a workaround or "as designed" - looks like the sample from the Readme is for a Shared Xamarin.Forms project, rather than a PCL implementation. I achieved the desired behavior by moving the pieces wrapped in #if __ANDROID__ to MainActivity.cs in my Android project, like so:

    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

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

        demoApp = new App();

        // This line is leveraging the android-specific implementation
        ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);

        LoadApplication(demoApp);
    }

All 4 comments

Yes, I had the same problem.
Fell back to using
var scanPage = new ZXingScannerPage();
which seems to work

Not sure if this is a workaround or "as designed" - looks like the sample from the Readme is for a Shared Xamarin.Forms project, rather than a PCL implementation. I achieved the desired behavior by moving the pieces wrapped in #if __ANDROID__ to MainActivity.cs in my Android project, like so:

    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

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

        demoApp = new App();

        // This line is leveraging the android-specific implementation
        ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);

        LoadApplication(demoApp);
    }

@JoeM-RP Just had the same problem, using the latest version of everything, and your solution still works for this problem _nearly a year later_. Well spotted. Maybe all the examples everywhere are also outdated :) Cheers

Thanks for reporting this issue! Unforunately it took me way too long to respond 馃槶. Sorry, I apologize! Recently the source code for this project was completely refactored to modernize it. Many PR's were included in this effort, and many bugs were hopefully fixed. Please try out the latest 3.x series of NuGet packages (currently in prerelease). To try and make the project more maintainable in my spare time going forward, I've decided to close all existing issues to start with a clean slate. If you're still experiencing this issue on the newest version, please open a new issue with as much detail as possible. Thank you for your patience and understanding! Happy scanning!

Was this page helpful?
0 / 5 - 0 ratings