2017-04-18 16 views
0

Visual Studioを使用して作成したAndriodアプリでZXingバーコードスキャナを実装しようとしています。ZXing Barcode Scanner Xamarin(Visual Studio)を使用したNullReferenceException

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

ですこれはこれはxamarinの私の最初のAndroidアプリです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> 

です。

+0

少なくとも、行番号を狭めておきますか? – Daniel

+0

行番号に何も言わなかった。文字通り、私にそのエラーメッセージを与えました。 –

答えて

0

"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()); 
} 
関連する問題