I have following code in my MainActivity file
Row
EMDKResults results = EMDKManager.GetEMDKManager(Android.App.Application.Context, this);
gives a runtime exception: Java.Lang.RuntimeException: 'stub' (NullReferenceException as I've found out with Google). How can I fix it? Thanks.
Google. Debugging. Rebuild. First-MAUI, than-Xamarin.
No one example from device sub-manufacturer (Zebra) doesn't work for me on device which I've could only detect starting and ending of scanning (Not with library, with ordinary broadcast event handler) (Atol Smart Slim Plus). May be anybody else has faced with it? Thank you.
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using AndroidX.AppCompat.App;
using Google.Android.Material.Snackbar;
using Symbol.XamarinEMDK;
using Symbol.XamarinEMDK.Barcode;
using System;
using System.Collections.Generic;
using static Symbol.XamarinEMDK.EMDKManager;
namespace ScanningXamarin
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity, IEMDKListener
{
private TextView statusView = null;
private TextView dataView = null;
EMDKManager emdkManager = null;
BarcodeManager barcodeManager = null;
Scanner scanner = null;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
statusView = FindViewById<TextView>(Resource.Id.statusView);
dataView = FindViewById<TextView>(Resource.Id.DataView);
EMDKResults results = EMDKManager.GetEMDKManager(Android.App.Application.Context, this);
if (results.StatusCode != EMDKResults.STATUS_CODE.Success)
{
statusView.Text = "Status: EMDKManager object creation failed ...";
}
else
{
statusView.Text = "Status: EMDKManager object creation succeeded ...";
}
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.Main);
AndroidX.AppCompat.Widget.Toolbar toolbar = FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
//FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
//fab.Click += FabOnClick;
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu_main, menu);
return true;
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
int id = item.ItemId;
if (id == Resource.Id.action_settings)
{
return true;
}
return base.OnOptionsItemSelected(item);
}
private void FabOnClick(object sender, EventArgs eventArgs)
{
View view = (View)sender;
Snackbar.Make(view, "Replace with your own action", Snackbar.LengthLong)
.SetAction("Action", (View.IOnClickListener)null).Show();
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
public void OnClosed()
{
statusView.Text = "Status: EMDK Open failed unexpectedly. ";
if (emdkManager != null)
{
emdkManager.Release();
emdkManager = null;
}
}
public void OnOpened(EMDKManager emdkManager)
{
statusView.Text = "Status: EMDK Opened successfully ...";
this.emdkManager = emdkManager;
InitScanner();
}
private void InitScanner()
{
if (emdkManager != null)
{
if (barcodeManager == null)
{
try
{
//Get the feature object such as BarcodeManager object for accessing the feature.
barcodeManager = (BarcodeManager)emdkManager.GetInstance(EMDKManager.FEATURE_TYPE.Barcode);
scanner = barcodeManager.GetDevice(BarcodeManager.DeviceIdentifier.Default);
if (scanner != null)
{
//Attahch the Data Event handler to get the data callbacks.
scanner.Data += scanner_Data;
//Attach Scanner Status Event to get the status callbacks.
scanner.Status += scanner_Status;
scanner.Enable();
//EMDK: Configure the scanner settings
ScannerConfig config = scanner.GetConfig();
config.SkipOnUnsupported = ScannerConfig.SkipOnUnSupported.None;
config.ScanParams.DecodeLEDFeedback = true;
config.ReaderParams.ReaderSpecific.ImagerSpecific.PickList = ScannerConfig.PickList.Enabled;
config.DecoderParams.Code39.Enabled = true;
config.DecoderParams.Code128.Enabled = false;
scanner.SetConfig(config);
}
else
{
displayStatus("Failed to enable scanner.\n");
}
}
catch (ScannerException e)
{
displayStatus("Error: " + e.Message);
}
catch (Exception ex)
{
displayStatus("Error: " + ex.Message);
}
}
}
}
void displayStatus(String status)
{
if (Looper.MainLooper.Thread == Java.Lang.Thread.CurrentThread())
{
statusView.Text = status;
}
else
{
RunOnUiThread(()=> statusView.Text = status);
}
}
void displaydata(string data)
{
if (Looper.MainLooper.Thread == Java.Lang.Thread.CurrentThread())
{
dataView.Text += (data + "\n");
}
else
{
RunOnUiThread(() => dataView.Text += data + "\n");
}
}
void DeinitScanner()
{
if (emdkManager != null)
{
if (scanner != null)
{
try
{
scanner.Data -= scanner_Data;
scanner.Status -= scanner_Status;
scanner.Disable();
}
catch (ScannerException e)
{
Log.Debug(this.Class.SimpleName, "Exception:" + e.Result.Description);
}
}
if (barcodeManager != null)
{
emdkManager.Release(EMDKManager.FEATURE_TYPE.Barcode);
}
barcodeManager = null;
scanner = null;
}
}
void scanner_Data(object sender, Scanner.DataEventArgs e)
{
ScanDataCollection scanDataCollection = e.P0;
if ((scanDataCollection != null) && (scanDataCollection.Result == ScannerResults.Success))
{
IList<ScanDataCollection.ScanData> scanData = scanDataCollection.GetScanData();
foreach (ScanDataCollection.ScanData data in scanData)
{
displaydata(data.LabelType + " : " + data.Data);
}
}
}
void scanner_Status(object sender, Scanner.StatusEventArgs e)
{
String statusStr = "";
//EMDK: The status will be returned on multiple cases. Check the state and take the action.
Symbol.XamarinEMDK.Barcode.StatusData.ScannerStates state = e.P0.State;
if (state == Symbol.XamarinEMDK.Barcode.StatusData.ScannerStates.Idle)
{
statusStr = "Scanner is idle and ready to submit read.";
try
{
if (scanner.IsEnabled && !scanner.IsReadPending)
{
scanner.Read();
}
}
catch (ScannerException e1)
{
statusStr = e1.Message;
}
}
if (state == Symbol.XamarinEMDK.Barcode.StatusData.ScannerStates.Waiting)
{
statusStr = "Waiting for Trigger Press to scan";
}
if (state == Symbol.XamarinEMDK.Barcode.StatusData.ScannerStates.Scanning)
{
statusStr = "Scanning in progress...";
}
if (state == Symbol.XamarinEMDK.Barcode.StatusData.ScannerStates.Disabled)
{
statusStr = "Scanner disabled";
}
if (state == Symbol.XamarinEMDK.Barcode.StatusData.ScannerStates.Error)
{
statusStr = "Error occurred during scanning";
}
displayStatus(statusStr);
}
protected override void OnResume()
{
base.OnResume();
InitScanner();
}
protected override void OnPause()
{
base.OnPause();
DeinitScanner();
}
protected override void OnDestroy()
{
base.OnDestroy();
//Clean up the emdkManager
if (emdkManager != null)
{
//EMDK: Release the EMDK manager object
emdkManager.Release();
emdkManager = null;
}
}
}
}
Please try to add
under application node in AndroidManifest.xml file