Redth / ZXing.Net.Mobile

Barcode Scanner for Xamarin.iOS, Xamarin.Android, UWP and Tizen
MIT License
1.07k stars 702 forks source link

Opening ZXing Failed: System.NullReferenceException #718

Open Magnabatt opened 6 years ago

Magnabatt commented 6 years ago

Hi All, I feel this might be a double up but i have not been able to find a solution for the below error. Please let me know if a solution has been found?

I have recently updated VS to 15.6.7 and subsequently updated to Xamrin.Forms 2.5.1.444934 with Zxing.Net.Mobile 2.4.1 using .NETStandard 2.0. Since doing this I now get a null reference error each time I call the script.

Also I have only seen the error on Android (testing iOS tonight)

Here is the error:

ERROR BtnScanQR_Clicked - Opening ZXing Failed: System.NullReferenceException: Object reference not set to an instance of an object. at ZXing.Mobile.MobileBarcodeScanner.GetContext (Android.Content.Context context) [0x0001f] in <57b37b10a314448c8d6b2b9155539100>:0 at ZXing.Mobile.MobileBarcodeScanner.Scan (Android.Content.Context context, ZXing.Mobile.MobileBarcodeScanningOptions options) [0x00015] in <57b37b10a314448c8d6b2b9155539100>:0 at ZXing.Mobile.MobileBarcodeScanner.Scan (ZXing.Mobile.MobileBarcodeScanningOptions options) [0x00001] in <57b37b10a314448c8d6b2b9155539100>:0 at ZXing.Mobile.MobileBarcodeScannerBase.Scan () [0x00006] in <5cc6d3dca85f4634ba661b03b5b196fa>:0 at App.MainPage+d__2.MoveNext () [0x00023] in C:\Users\Admin\source\repos\App\App\App\MainPage.xaml.cs:67

In my MainActivity.cs I call the following after my Forms.Int: //QR code reader global::ZXing.Net.Mobile.Forms.Android.Platform.Init();

And this is the script with in my click event on the MainPage.cs:

//Open QR scanner for code scanning private async void BtnScanQR_Clicked(object sender, EventArgs e) { try { var scanner = new MobileBarcodeScanner(); var result = await scanner.Scan(); strAccessToken.Text = result.Text.Trim(); } catch (Exception ex) { GlobalScript.DebugMessages("ERROR", "BtnScanQR_Clicked", "Opening ZXing Failed: " + ex); } }

Even rolling back to my original versions of each package I'm now still having the same error.

Any assistance would be appretiated

chiragjuneja commented 6 years ago

I am also facing the similar crash when I try to open the scan page. I noticed that it is happening once I upgraded to Xamarin.forms 2.5.0.* version.

[ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.NullPointerException: Attemptinvoke virtual method 'android.content.res.Resources android.content.Context.getResources()' oa null objecreference aSystem.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] i:0 aJava.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfmethod, Java.Interop.JniArgumentValue args) [0x00089] i<3cb12bce834a498199b2be6cd3e00922>:0 aJava.Interop.JniPeerMembers+JniInstanceMethods.FinishCreateInstance (System.String constructorSignature, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue parameters) [0x0004f] i<3cb12bce834a498199b2be6cd3e00922>:0 aAndroid.Views.SurfaceView..cto(Android.Content.Contexcontext) [0x00070] i/Users/builder/data/lanes/5809/a7829590/source/monodroid/external/xamarin-android/src/Mono.Android/obj/Release/android-25/mcw/Android.Views.SurfaceView.cs:48 aZXing.Mobile.ZXingSurfaceView..cto(Android.App.Activity activity, ZXing.Mobile.MobileBarcodeScanningOptions options) [0x00041] i<04a6c4a6536f4e76a10998845fe4c337>:0 aZXing.Net.Mobile.Forms.Android.ZXingScannerViewRenderer+d__5.MoveNex() [0x000fd] i<30849edafd8e4fbb9f24af588a9bebb0>:0

F0rc0sigan commented 6 years ago

I am getting the same issue

chiragjuneja commented 6 years ago

Any update on this? We can't move back to Xamarin.forms 2.4 and QR code scanning is not working for with Xamarin.forms 2.5.

knight1219 commented 6 years ago

I have having the same issue: VS 15.7.3, Xamarin Forms 3.0.0.530983, ZXing 2.4.1

Has anyone solved this?

Magnabatt commented 6 years ago

Hi All,

Sorry for the delay. I have managed to get this working by updating my on click action to the below;

//Open QR scanner for code scanning private async void BtnScanQR_Clicked(object sender, EventArgs e) { try { var options = new MobileBarcodeScanningOptions { AutoRotate = false, UseFrontCameraIfAvailable = false, TryHarder = true };

var overlay = new ZXingDefaultOverlay { TopText = "Please scan QR code", BottomText = "Align the QR code within the frame" };

var QRScanner = new ZXingScannerPage(options,overlay);

await Navigation.PushModalAsync(QRScanner);

QRScanner.OnScanResult += (result) => { // Stop scanning QRScanner.IsScanning = false;

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

};

} catch (Exception ex) { GlobalScript.SeptemberDebugMessages("ERROR", "BtnScanQR_Clicked", "Opening ZXing Failed: " + ex); } }

Hope this works for you. Also if you find any improvements in the above please share with me.

Packages: Xamarin.Forms 2.5.1.52743 ZXing.Net.Mobile.Forms 2.3.2

suyana commented 6 years ago

@knight1219

if you used mvvmcross 6 ?? add this code at MainActivity Xamarin.Forms.Forms.Init(this, bundle);

pablocity commented 6 years ago

@suyana That actually worked! I was facing this error for a while, so thank you very much :)

danylo-kachanov commented 5 years ago

@suyana thank you! it helps me too!

gcoleman0828 commented 5 years ago

ZXing.Net.Mobile.Forms.Android.Platform.Init(); Did not work for me. the newest version to date (12/31/2018) v2.4.1 still crashes. MobileBarcodeScanner.Initialize(Application); works adding to MainActivity.cs

ZXing.Net.Mobile: 2.4.1 ZXing.Net.Mobile.Forms: 2.4.1 Xamarin.Form: 3.4.0.1008975 Xamarin.Android.xxx: 28.0.0

Maxallica commented 5 years ago

I got the same issue. Please can you fix this critical issue!

UNHANDLED EXCEPTION: Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue args) [0x00089] in :0 at Java.Interop.JniPeerMembers+JniInstanceMethods.FinishCreateInstance (System.String constructorSignature, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue parameters) [0x0004f] in :0 at Android.Views.SurfaceView..ctor (Android.Content.Context context) [0x0007a] in <31c0593de1b74193a8262ad9506cfb8a>:0 at ZXing.Mobile.ZXingSurfaceView..ctor (Android.Content.Context context, ZXing.Mobile.MobileBarcodeScanningOptions options) [0x00007] in <819b29aa6d91462699e19a679be55a44>:0 at ZXing.Net.Mobile.Forms.Android.ZXingScannerViewRenderer+\<OnElementChanged>d5.MoveNext () [0x000cf] in <6a1930ceea034eb28eda2834838f88c6>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b6_0 (System.Object state) [0x00000] in <81fa13418db94ea2be5a18084a151e08>:0 at Android.App.SyncContext+<>cDisplayClass2_0.b__0 () [0x00000] in <31c0593de1b74193a8262ad9506cfb8a>:0 at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <31c0593de1b74193a8262ad9506cfb8a>:0 at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr nativethis) [0x00009] in <31c0593de1b74193a8262ad9506cfb8a>:0 at (wrapper dynamic-method) System.Object.31(intptr,intptr) --- End of managed Java.Lang.NullPointerException stack trace --- java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.view.ViewConfiguration.get(ViewConfiguration.java:469) at android.view.View.(View.java:4867) at android.view.View.(View.java:5054) at android.view.SurfaceView.(SurfaceView.java:206) at android.view.SurfaceView.(SurfaceView.java:202) at android.view.SurfaceView.(SurfaceView.java:198) at android.view.SurfaceView.(SurfaceView.java:194) at md5bb098716dd46c8e113564e6b42b7cde9.ZXingSurfaceView.(ZXingSurfaceView.java:28) at md58432a647068b097f9637064b8985a5e0.FragmentContainer.n_onCreateView(Native Method) at md58432a647068b097f9637064b8985a5e0.FragmentContainer.onCreateView(FragmentContainer.java:33) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:391) at android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:517) at md51558244f76c53b6aeda52c8a337f2c37.FormsAppCompatActivity.n_onResume(Native Method) at md51558244f76c53b6aeda52c8a337f2c37.FormsAppCompatActivity.onResume(FormsAppCompatActivity.java:112) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1363) at android.app.Activity.performResume(Activity.java:7440) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3780) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3845) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1773) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:7000) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

ZXing.Net.Mobile: 2.4.1 ZXing.Net.Mobile.Forms: 2.4.1 Xamarin.Forms: 3.5.0.129452 Xamarin.Android.Support.Annotations: 28.0.0.1 Xamarin.Android.Support.Compat: 28.0.0.1 Xamarin.Android.Support.Core.UI: 28.0.0.1 Xamarin.Android.Support.Design: 28.0.0.1 Xamarin.Android.Support.Fragment: 28.0.0.1 Xamarin.Android.Support.Media.Compat: 28.0.0.1 Xamarin.Android.Support.v4: 28.0.0.1 Xamarin.Android.Support.v7.AppCompat: 28.0.0.1 Xamarin.Android.Support.v7.CardView: 28.0.0.1 Xamarin.Android.Support.v7.MediaRouter: 28.0.0.1 Xamarin.Android.Support.v7.RecyclerView: 28.0.0.1

jramoseguinoa commented 5 years ago

Using @Maxallica commit I was able to fix this issue. I created a ViewRenderer (using the code that is referenced in his fork) in my android project that replaced the one in the library and now the issue is gone. Thanks for the fix @Maxallica !

bingxueyinlian commented 4 years ago

@suyana thank you! it helps me too!