2017-04-18 2 views
0

Visual Studio를 사용하여 작성한 Andriod 응용 프로그램에서 ZXing 바코드 스캐너를 구현하려고합니다.ZXing 바코드 스캐너 NullReferenceException - Xamarin (Visual Studio)

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> 

입니다.

+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()); 
} 
관련 문제