c# - ZXing 条码扫描仪 NullReferenceException 与 Xamarin (Visual Studio)

标签 c# android visual-studio xamarin xamarin.android

我正在尝试在使用 Visual Studio 构建的 Andriod 应用程序上实现 ZXing 条形码扫描仪。当我尝试单击按钮进行扫描时出现此错误:

System.NullReferenceException: Object reference not set to an instance of an object.

这是 ScanActivity.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using ZXing.Mobile;

namespace Shopinator
{
[Activity(Label = "ScanActivity")]

public class ScanActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.ScanLayout);
        Button scanCodeSrc = FindViewById<Button>(Resource.Id.scanCode);
        Button goBackSrc = FindViewById<Button>(Resource.Id.goBackBtn);

        scanCodeSrc.Click += scanCodeSrc_Click;
        goBackSrc.Click += goBackSrc_Click;
    }

    public async void scanCodeSrc_Click(object sender, EventArgs e)
    {
        MobileBarcodeScanner.Initialize(Application);
        MobileBarcodeScanner scanner = new MobileBarcodeScanner();
        var result = await scanner.Scan();
        if (result != null)
            Console.WriteLine("Scanned Barcode: " + result.Text);
    }

    private void goBackSrc_Click(object sender, EventArgs e)
    {
        StartActivity(typeof(MainMenuActivity));
    }
}
}

这是 ScanLayout.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2579BF"
android:orientation="vertical">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_margin="10dp"
    android:orientation="vertical">
    <TextView
        android:text="Welcome to the Product Scan page, please press the scan button and proceed with scanning the barcode/qrcode."
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scanPageHelp" />
    <Button
        android:id="@+id/scanCode"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/password"
        android:layout_marginTop="10dp"
        android:background="#000000"
        android:text="PRESS HERE TO SCAN"
        android:textColor="@android:color/white" />
    <Button
        android:id="@+id/goBackBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/password"
        android:layout_marginTop="10dp"
        android:background="#000000"
        android:text="Go back to the Main Menu"
        android:textColor="@android:color/white" />
</LinearLayout>

这是我在 xamarin 上的第一个 Android 应用。

最佳答案

OnCreate 中 MainActivity.cs 中的 Android 项目:

ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);

关于c# - ZXing 条码扫描仪 NullReferenceException 与 Xamarin (Visual Studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43463081/

相关文章:

c# 将 LINQ var 结果转换为实际类型

android - 如何检查用户在 EditText 中输入自己的电话号码?

android - ListView 回收屏幕上可见的 View

java - 如何对 recyclerView 进行分组,在组之间添加分隔线并为每个组添加一个图标

c++ - 为什么我应该使用C++/CLI类型而不是C++ Native?

javascript/jquery 无法在部分 View 中工作。

c# - 尝试将 C# 函数移植到 PHP5

visual-studio - GetEnvironmentVariable 应该在 xUnit 测试中工作吗?

c# - C# 中的 C++ dll

c++ - Visual Studio 2015 上的日志