首页 文章

ZXing条码扫描器NullReferenceException与Xamarin(视觉工作室)

提问于
浏览
1

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

System.NullReferenceException:未将对象引用设置为对象的实例 .

This is 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));
    }
}
}

This is 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应用程序 .

2 回答

  • 2

    OnCreate中MainActivity.cs中的Android项目:

    ZXing.Mobile.MobileBarcodeScanner.Initialize(应用程序);

  • 0

    如果您只是从头开始接线,那么"MainPage"将不会出现在导航环境中并且会像这样死亡 . 将您的MainPage放在导航上下文中 . 在这里完成Zxing的"simple"版本 - https://github.com/jhealy/xammutts/tree/master/ZXingPlaySLN/ZXingPlay .

    public App()
    {
        InitializeComponent();
    
        // jhealy: this MUST be a nav page, watch out
        // MainPage = new ZXingPlay.MainPage();
        MainPage = new NavigationPage(new ZXingPlay.MainPage());
    }
    

相关问题