【问题标题】:ZXing Barcode Scanner NullReferenceException with Xamarin (visual studio)ZXing Barcode Scanner NullReferenceException with Xamarin (visual studio)
【发布时间】:2017-09-13 18:08:41
【问题描述】:

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

System.NullReferenceException:对象引用未设置为实例 一个对象。

这是 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 应用。

【问题讨论】:

  • 你至少有一行#来帮助缩小范围吗?
  • 它没有说任何与行号相关的内容。它实际上只是给了我那个错误信息。

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


【解决方案1】:

OnCreate 中 MainActivity.cs 中的 Android 项目:

ZXing.Mobile.MobileBarcodeScanner.Initialize(Application);

【讨论】:

    【解决方案2】:

    如果您只是从头开始连接,您的“MainPage”将不在导航上下文中,并且会像这样死掉。将您的 MainPage 放在导航上下文中。在此处完成 Zxing 的“简单”版本 - 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());
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-06
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多