0

I am using the ZXing.Net.Mobile NuGet package for scanning barcode numbers. I have done everything as per this blog.

My Code

//Main Project Interface
public interface IQrScanningService  
{  
   Task<string> ScanAsync();  
}

//Android part implementation
[assembly: Dependency(typeof(XFBarcode.Droid.Services.QrScanningService))]  
namespace projectname.Droid.Services  
{  
    public class QrScanningService : IQrScanningService  
    {  
        public async Task<string> ScanAsync()  
        {  
            var optionsDefault = new MobileBarcodeScanningOptions();  
            var optionsCustom = new MobileBarcodeScanningOptions();  
  
            var scanner = new MobileBarcodeScanner()  
            {  
                TopText = "Scan the QR Code",  
                BottomText = "Please Wait",  
            };  
  
            var scanResult = await scanner.Scan(optionsCustom);  
            return scanResult.Text;  
        }  
    }  
}  

But when I execute I am getting System.NullReferenceException: 'Object reference not set to an instance of an object.'. What else I am missing here? I saw a similar thread here but don't how to download use a package from GitHub.

Sreejith Sree
  • 3,055
  • 4
  • 36
  • 105
  • what line throws the exception? what is the stack trace? What version of the nuget package are you using? Android or iOS? – Jason Feb 24 '21 at 15:58
  • In that blog they don't set this init line: https://github.com/jfversluis/WifiBarcodeSample/blob/master/WifiBarcodeSample.Android/MainActivity.cs#L17 Did you do that? Also see: https://www.dotnetcurry.com/xamarin/1490/barcode-xamarin-apps – Gerald Versluis Feb 24 '21 at 16:36

1 Answers1

1

You missed the initialization step.

Try this:

public async Task<string> ScanAsync()
    {
        //Initialize the scanner first so it can track the current context
        MobileBarcodeScanner.Initialize(MainActivity.Instance.Application);

        var optionsDefault = new MobileBarcodeScanningOptions();
        var optionsCustom = new MobileBarcodeScanningOptions();

        var scanner = new MobileBarcodeScanner()
        {
            TopText = "Scan the QR Code",
            BottomText = "Please Wait",
        };

        var scanResult = await scanner.Scan(optionsCustom);
        return scanResult.Text;
    }  

in your MainActivity :

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static MainActivity Instance;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);
        Instance = this;
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }

}

You also could initialize in MainActivity OnCreate() method directly.

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

    base.OnCreate(savedInstanceState);
    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    MobileBarcodeScanner.Initialize(Application); //initialize  here
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    LoadApplication(new App());
}
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • Can you check this thread? https://stackoverflow.com/questions/66850878/xamarin-forms-how-to-remove-empty-space-between-listview-items-based-on-date – Sreejith Sree Mar 29 '21 at 09:21