Xamarin.Forms Zxing barcode scanner - errors at new ZXingScannerPage()

robs23robs23 Member ✭✭

Hi Guys,

I'm going nuts trying to get it to work. I have a simple app making use of Zxing barcode scanner library. It basically has 1 form with btnScan button and txtBarcode textbox on it. When I click the button, this event is to take place:

    private async void btnScan_Clicked(object sender, EventArgs e)
            {
    scanPage = new ZXingScannerPage();
            scanPage.AutoFocus();
            scanPage.OnScanResult += (result) =>
            {
                // Stop scanning
                scanPage.IsScanning = false;
                scanPage.AutoFocus(0, 500);

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

            await Navigation.PushModalAsync(scanPage);

The thing is I keep getting "Value cannot be null" exception at scanPage = new ZXingScannerPage()

The code above is located in MainPage.xaml.cs. In the file there's also:

ZXingScannerPage scanPage;

        public MainPage()
        {
            InitializeComponent();
            btnScan.Clicked += btnScan_Clicked;

        }

And in Android's part of the project, in MainActivity.cs:

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

            base.OnCreate(bundle);

            //// Initialize the scanner first so we can track the current context
            //MobileBarcodeScanner.Initialize(Application);
            ZXing.Net.Mobile.Forms.Android.Platform.Init();
            global::Xamarin.Forms.Forms.Init(this, bundle);

            formsApp = new App();
            LoadApplication(formsApp);
        }

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

This is really simple app so far, yet still I can't get it to work :neutral: Please help!

Robert

Best Answer

Answers

  • robs23robs23 Member ✭✭

    @AlessandroCaliaro First, thank you for the answer, you bring back the hope :smile: Although Xamarin is very promising technology, I find it rather difficult to start with. I downloaded your project but in error list I see 1 error that reads as in code section below. Surprisinlgy enough, though, the project builds and I can test it on my phone through live player function. When it opens on the phone, I can see a form with "Welcome.." label and "Open scan page" button. Unfortunately, when the button is clicked nothing happens.. Maybe there's something wrong with my setup? I mean, maybe I haven't installed right Android SDK or something?

    An error occurred trying to install required android components on Project 'TestZXing.Droid'.
    Project 'TestZXing.Droid' requires the following components installed on your machine: 
    Xamarin.Android.Support.v4
    JavaLibraryReference: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/support-v4/23.3.0/support-v4-23.3.0.aar-23.3.0.0
    JavaLibraryReference: link://dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/support-v4/23.3.0/support-v4-23.3.0.aar-23.3.0.0
    AndroidResources: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/support-v4/23.3.0/support-v4-23.3.0.aar-23.3.0.0
    Xamarin.Android.Support.Vector.Drawable
    JavaLibraryReference: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/support-vector-drawable/23.3.0/support-vector-drawable-23.3.0.aar-23.3.0.0
    AndroidResources: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/support-vector-drawable/23.3.0/support-vector-drawable-23.3.0.aar-23.3.0.0
    Xamarin.Android.Support.Animated.Vector.Drawable
    JavaLibraryReference: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/animated-vector-drawable/23.3.0/animated-vector-drawable-23.3.0.aar-23.3.0.0
    AndroidResources: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/animated-vector-drawable/23.3.0/animated-vector-drawable-23.3.0.aar-23.3.0.0
    Xamarin.Android.Support.v7.AppCompat
    JavaLibraryReference: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/appcompat-v7/23.3.0/appcompat-v7-23.3.0.aar-23.3.0.0
    AndroidResources: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/appcompat-v7/23.3.0/appcompat-v7-23.3.0.aar-23.3.0.0
    Xamarin.Android.Support.v7.RecyclerView
    JavaLibraryReference: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/recyclerview-v7/23.3.0/recyclerview-v7-23.3.0.aar-23.3.0.0
    AndroidResources: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/recyclerview-v7/23.3.0/recyclerview-v7-23.3.0.aar-23.3.0.0
    Xamarin.Android.Support.Design
    JavaLibraryReference: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/design/23.3.0/design-23.3.0.aar-23.3.0.0
    AndroidResources: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/design/23.3.0/design-23.3.0.aar-23.3.0.0
    Xamarin.Android.Support.v7.CardView
    JavaLibraryReference: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/cardview-v7/23.3.0/cardview-v7-23.3.0.aar-23.3.0.0
    AndroidResources: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/cardview-v7/23.3.0/cardview-v7-23.3.0.aar-23.3.0.0
    Xamarin.Android.Support.v7.MediaRouter
    JavaLibraryReference: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/mediarouter-v7/23.3.0/mediarouter-v7-23.3.0.aar-23.3.0.0
    JavaLibraryReference: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/mediarouter-v7/23.3.0/mediarouter-v7-23.3.0.aar-23.3.0.0
    AndroidResources: //dl-ssl.google.com/android/repository/android_m2repository_r29.zip-m2repository/com/android/support/mediarouter-v7/23.3.0/mediarouter-v7-23.3.0.aar-23.3.0.0
    
    
    Please double-click here to install it.
    
    Intallation Errors: XA5209 Unzipping failed. Please download '//dl-ssl.google.com/android/repository/android_m2repository_r29.zip' and extract it to the 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.v4\23.3.0.0\content' directory
    XA5209 Reason: Could not find a part of the path 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.v4\23.3.0.0\content\m2repository\'.
    XA5207 Please install package: 'Xamarin.Android.Support.v4' available in SDK installer. Java library file 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.v4\23.3.0.0\content\classes.jar' doesn't exist.
    XA5207 Please install package: 'Xamarin.Android.Support.v4' available in SDK installer. Java library file 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.v4\23.3.0.0\content\libs/internal_impl-23.3.0.jar' doesn't exist.
    XA5207 Please install package: 'Xamarin.Android.Support.Vector.Drawable' available in SDK installer. Java library file 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.Vector.Drawable\23.3.0.0\content\classes.jar' doesn't exist.
    XA5207 Please install package: 'Xamarin.Android.Support.Animated.Vector.Drawable' available in SDK installer. Java library file 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.Animated.Vector.Drawable\23.3.0.0\content\classes.jar' doesn't exist.
    XA5207 Please install package: 'Xamarin.Android.Support.v7.AppCompat' available in SDK installer. Java library file 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.v7.AppCompat\23.3.0.0\content\classes.jar' doesn't exist.
    XA5207 Please install package: 'Xamarin.Android.Support.v7.RecyclerView' available in SDK installer. Java library file 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.v7.RecyclerView\23.3.0.0\content\classes.jar' doesn't exist.
    XA5207 Please install package: 'Xamarin.Android.Support.Design' available in SDK installer. Java library file 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.Design\23.3.0.0\content\classes.jar' doesn't exist.
    XA5207 Please install package: 'Xamarin.Android.Support.v7.CardView' available in SDK installer. Java library file 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.v7.CardView\23.3.0.0\content\classes.jar' doesn't exist.
    XA5207 Please install package: 'Xamarin.Android.Support.v7.MediaRouter' available in SDK installer. Java library file 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.v7.MediaRouter\23.3.0.0\content\classes.jar' doesn't exist.
    XA5207 Please install package: 'Xamarin.Android.Support.v7.MediaRouter' available in SDK installer. Java library file 'C:\Users\robert.roszak\AppData\Local\Xamarin\Xamarin.Android.Support.v7.MediaRouter\23.3.0.0\content\libs/internal_impl-23.3.0.jar' doesn't exist.           0   
    
  • robs23robs23 Member ✭✭

    To admins: can you maybe take this silly restriction off that's saying "You have to be around a bit longer to post links"? I expect no one is going make a link farm out of this site immediately but rather use them wisely.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    For me it works... I have updated to last XF forms and last ZXing libs. Commited to github. Try to clone again the project

  • robs23robs23 Member ✭✭

    Hi,

    So I updated Xamarin.Forms and Zsing libraries, but then I'm getting 9 errors:

    Error       No resource found that matches the given name: attr 'windowNoTitle'.    TestZXing.Droid C:\Inne\Programowanie\TestZXing\TestZXing-master\Droid\Resources\values\styles.xml  2   
    Error       Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'.  TestZXing.Droid C:\Inne\Programowanie\TestZXing\TestZXing-master\Droid\Resources\values\styles.xml  2   
    Error       No resource found that matches the given name: attr 'colorPrimary'. TestZXing.Droid C:\Inne\Programowanie\TestZXing\TestZXing-master\Droid\Resources\values\styles.xml  2   
    Error       No resource found that matches the given name: attr 'windowActionBar'.  TestZXing.Droid C:\Inne\Programowanie\TestZXing\TestZXing-master\Droid\Resources\values\styles.xml  2   
    Error       No resource found that matches the given name: attr 'colorPrimaryDark'. TestZXing.Droid C:\Inne\Programowanie\TestZXing\TestZXing-master\Droid\Resources\values\styles.xml  2   
    Error       No resource found that matches the given name: attr 'colorAccent'.  TestZXing.Droid C:\Inne\Programowanie\TestZXing\TestZXing-master\Droid\Resources\values\styles.xml  3   
    Error       No resource found that matches the given name: attr 'colorAccent'.  TestZXing.Droid C:\Inne\Programowanie\TestZXing\TestZXing-master\Droid\Resources\values\styles.xml  4   
    Error       Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.Dialog'. TestZXing.Droid C:\Inne\Programowanie\TestZXing\TestZXing-master\Droid\Resources\values\styles.xml  4   
    Error       No resource found that matches the given name: attr 'windowActionModeOverlay'.  TestZXing.Droid C:\Inne\Programowanie\TestZXing\TestZXing-master\Droid\Resources\values\styles.xml  4   
    

    All are associated with Droid\Resources\values\styles.xml. I can see in NuGet that there are still 16 packages to update (please have a look at list below). I tried to update them all at once and it was looking like doing someithng, but at the end I still have 16 packages to update (all the same). Maybe I should udpate them 1 by 1 but in what sequence? Many of them link to one another so I guess sequence might be important here. I feel like I'm missing some basic principles of developing for Android (like which SDK to install if you target e.g. Android 6.0 device, which other libraries need to be installed along it and in what version.. Could you maybe recommend some materials on that? I think I won't get ahead if I don't take my time to catch on basics..

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think you have to update Android SDK

  • robs23robs23 Member ✭✭

    Thanks for keeping this going.

    I installed 3 updates that Android SDK Manager found, but it changed nothing. Then I found some post suggesting deleting everything from bin, obj and packages folders might help. I did that and then I was able to build successfully without errors. Testing through live player and the app got stuck after "Open scan page", just as at the first time. Grasping at straws I decided to create APK file out of the project and test install it on my device. It installed, opened and it's working. Can't believe it does! Finally! So much frustration..
    I guess I'll have to build my small app entirely on your project then. Thank you for all your help and support!

  • ThamaraiThamarai Member ✭✭

    Hi,
    How can we use this plugin for cross-platform android app by xamarin? Is it possible?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Yes it is . You can use it with Xamarin Forms
  • ThamaraiThamarai Member ✭✭

    Hi Alessandro Caliaro,
    Currently I am developing an android mobile application in Xamarin.Android (not Xamarin.Forms), in which I need the feature of scanning barcode through device camera. Is it possible with ZXing.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @Thamarai said:
    Hi Alessandro Caliaro,
    Currently I am developing an android mobile application in Xamarin.Android (not Xamarin.Forms), in which I need the feature of scanning barcode through device camera. Is it possible with ZXing.

    never used, but I think yes, it's possible. https://github.com/Redth/ZXing.Net.Mobile/tree/master/Samples/Android

  • ThamaraiThamarai Member ✭✭

    @AlessandroCaliaro said:

    @Thamarai said:
    Hi Alessandro Caliaro,
    Currently I am developing an android mobile application in Xamarin.Android (not Xamarin.Forms), in which I need the feature of scanning barcode through device camera. Is it possible with ZXing.

    never used, but I think yes, it's possible. https://github.com/Redth/ZXing.Net.Mobile/tree/master/Samples/Android

    Thanks for the reply. I will check this and update you. Thank you once again.

  • ThamaraiThamarai Member ✭✭

    Hi Alessandro Caliaro,
    It works. Thank you for this help.

  • hyderabadihyderabadi USMember ✭✭✭

    @AlessandroCaliaro it s working fine. Thanks.

Sign In or Register to comment.