添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to use the NFC technology for my Android application.

I want the users to be able to scan an NFC card so they can log in to the application. The card contains a number.

So the idea is just to retrieve the code and convert it to string

but I'm getting an Nullobjectreference

Attempt to invoke virtual method 'boolean android.nfc.Tag.hasTech(int)' on a null object reference

UPDATE

Source code:

using System;
using System.Text;
using Android.App;
using Android.Nfc;
using Android.OS;
using Android.Widget;
using Android.Content;
using Android.Nfc.Tech;
namespace NFC_NDEF
[Activity(Label = "NFC_NDEF_TST", MainLauncher = true, Icon = "@drawable/icon")]
[IntentFilter(new[] { NfcAdapter.ActionTechDiscovered })]
public class MainActivity : Activity 
NfcAdapter nfcAdapter;
PendingIntent nfcPi;
IntentFilter nfcFilter;
Tag nfcTag;
string newLine = System.Environment.NewLine;
protected override void OnCreate(Bundle bundle)
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);
    // Get our button from the layout resource,
    // and attach an event to it
    Button btnScan = FindViewById<Button>(Resource.Id.MyButton);
    btnScan.Click += Scan;
    //var writerButton = FindViewById<Button>(Resource.Id.WriteButton);
    //writerButton.Click += Write;
    var label = FindViewById<TextView>(Resource.Id.textView_rslt);
    nfcAdapter = NfcAdapter.GetDefaultAdapter(ApplicationContext);
    if (nfcAdapter == null)
        label.Text = "NFC is not available.";
        return;
    if (!nfcAdapter.IsEnabled)
        label.Text = "NFC is disabled.";
        return;
    var intent = new Intent(this, this.Class);
    intent.AddFlags(ActivityFlags.SingleTop);
    nfcPi = PendingIntent.GetActivity(this, 0, intent, 0);
    nfcFilter = new IntentFilter(NfcAdapter.ActionTechDiscovered);
    nfcFilter.AddCategory(Intent.CategoryDefault);
private void Scan(object sender, EventArgs e)
    var label = FindViewById<TextView>(Resource.Id.textView_rslt);
        if (nfcTag == null)
            label.Text = "nfc tag is null";
            return;
        var ndef = Ndef.Get(nfcTag);
        ndef.Connect();
        var data = Encoding.UTF8.GetString(ndef.NdefMessage.ToByteArray());
        ndef.Close();
        label.Text = $"Data:{newLine}{data}";
    catch (Exception ex)
        label.Text += $"{newLine} Exception: {newLine} {ex.Message} {newLine} {ex.StackTrace}";
protected override void OnResume()
    base.OnResume();
    nfcAdapter.EnableForegroundDispatch(this, nfcPi, new IntentFilter[] { nfcFilter }, null);
    if (NfcAdapter.ActionTechDiscovered == Intent.Action)
        ProcessIntent(Intent);
protected override void OnNewIntent(Intent intent)
    base.OnNewIntent(intent);
    Intent = intent;
    if (NfcAdapter.ActionTechDiscovered == intent.Action)
        ProcessIntent(Intent);
private void ProcessIntent(Intent intent)
    var label = FindViewById<TextView>(Resource.Id.textView_rslt);
        nfcTag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
        label.Text = $"Id: 0x{Smartisan.Nfc.Utils.ByteToHex(nfcTag.GetId())}";
        label.Text += $"{newLine}Techs: {newLine}";
        label.Text += string.Join(newLine, nfcTag.GetTechList());
    catch (Exception ex)
        label.Text += $"{newLine} Exception: {newLine} {ex.Message} {newLine} {ex.StackTrace}";

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="NFC_NDEF.NFC_NDEF" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:targetSdkVersion="15" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
<application android:label="NFC_NDEF"></application>
</manifest>
                Thank u for ur time, please read the post i updated it with details, source code and exception
– KARIM Yassine
                Dec 9, 2016 at 11:44
                the if (nfcTag == null) { label.Text = "nfc tag is null";return; } it's like that i have to add a block for unknow type, actually im beginner with xamarin, and im not sure where is the error
– KARIM Yassine
                Dec 13, 2016 at 10:08
                According to the error message, when you call "hasTech" function the nfcTag is null. I think you may use the nfcTag object before it initialized.
– Mike Ma
                Dec 14, 2016 at 6:35
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.