Android NFC - 尝试在空对象引用上调用虚拟方法 'boolean android.nfc.Tag.hasTech(int)'

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

我正尝试在 Android 上通过 NFC 读取智能卡。我有一个在 Eclipse 中工作的解决方案,但在迁移到 Android Studio 后它只工作一次,从卡中读取所有数据,但随后冻结并一遍又一遍地重复此错误:

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

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.nfc.Tag.hasTech(int)' on a null object reference

W/System.err: at android.nfc.tech.IsoDep.get(IsoDep.java:61)

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="packageName"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="23" />

    <uses-permission android:name="android.permission.NFC" />

    <uses-feature
        android:name="android.hardware.nfc"
        android:required="false" />

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true" >

        <activity
            android:name=".NFCReader"
            android:label="@string/app_name"
            android:launchmode="singleTask" >

            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>

            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>
    </application>

</manifest>

NFC 阅读器:

public class NFCReader extends Activity {

private NfcAdapter mNfcAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    if (mNfcAdapter == null) {
        finish();
        return;
    }

    if (!mNfcAdapter.isEnabled()) {
        Log.e(TAG, "NFC is disabled.");
    } else {
    }

    final Tag tagFromIntent = this.getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
    new ReadCardTask().execute(tagFromIntent);

    handleIntent(getIntent());
    this.finish();
}

private void handleIntent(Intent intent) {
    String action = intent.getAction();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        String type = intent.getType();
        if (MIME_TEXT_PLAIN.equals(type)) {
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            new NdefReaderTask().execute(tag);
        } else {
            Log.d(TAG, "Wrong mime type: " + type);
        }
    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        String[] techList = tag.getTechList();
        String searchedTech = Ndef.class.getName();

        for (String tech : techList) {
            if (searchedTech.equals(tech)) {
                new NdefReaderTask().execute(tag);
                break;
            }
        }
    }
}

protected Void doInBackground(Tag... params) {
    IsoDep tag = IsoDep.get(params[0]);
    tag.connect();
    // [...] transmit APDUs
}

}

出于某种原因,doInBackground() 函数被调用了两次,一次是在读取有效的标签中,但第二次是在标签中调用了 null,这导致了错误。对标记参数的空检查修复了它:

protected Void doInBackground(Tag... params) {
    if (params[0] == null)
    {
        return null;
    }

    IsoDep tag = IsoDep.get(params[0]);
    tag.connect();
    // [...] transmit APDUs
}