From b5073e71c4652b309351f1dbbf2211ecd4cf5f58 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Sat, 17 Aug 2019 20:49:10 +0200 Subject: [PATCH] Error: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.nfc.Tag.hasTech(int)' on a null object reference #40 --- src/nfc.android.ts | 20 ++++++++++++-------- src/package.json | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/nfc.android.ts b/src/nfc.android.ts index 3491af2..c865886 100644 --- a/src/nfc.android.ts +++ b/src/nfc.android.ts @@ -42,7 +42,6 @@ export class NfcIntentHandler { // every action should map to a different listener you pass in at 'startListening' if (action === android.nfc.NfcAdapter.ACTION_NDEF_DISCOVERED) { - console.log(">> tag: " + tag); // issue 40? let ndef = android.nfc.tech.Ndef.get(tag); let ndefJson: NfcNdefData = this.ndefToJSON(ndef); @@ -354,13 +353,13 @@ export class Nfc implements NfcApi { public eraseTag(): Promise { return new Promise((resolve, reject) => { - let intent = application.android.foregroundActivity.getIntent(); - if (intent === null || nfcIntentHandler.savedIntent === null) { + const intent = application.android.foregroundActivity.getIntent() || nfcIntentHandler.savedIntent; + if (!intent) { reject("Can't erase tag; didn't receive an intent"); return; } - let tag = nfcIntentHandler.savedIntent.getParcelableExtra(android.nfc.NfcAdapter.EXTRA_TAG) as android.nfc.Tag; + let tag = intent.getParcelableExtra(android.nfc.NfcAdapter.EXTRA_TAG) as android.nfc.Tag; let records = new Array.create(android.nfc.NdefRecord, 1); let tnf = android.nfc.NdefRecord.TNF_EMPTY; @@ -389,13 +388,18 @@ export class Nfc implements NfcApi { reject("Nothing passed to write"); return; } - let intent = application.android.foregroundActivity.getIntent(); - if (intent === null || nfcIntentHandler.savedIntent === null) { - reject("Can not write to tag; didn't receive an intent"); + + const intent = application.android.foregroundActivity.getIntent() || nfcIntentHandler.savedIntent; + if (!intent) { + reject("Can't write to tag; didn't receive an intent"); return; } - let tag = nfcIntentHandler.savedIntent.getParcelableExtra(android.nfc.NfcAdapter.EXTRA_TAG) as android.nfc.Tag; + let tag = intent.getParcelableExtra(android.nfc.NfcAdapter.EXTRA_TAG) as android.nfc.Tag; + if (!tag) { + reject("No tag found to write to"); + return; + } let records = this.jsonToNdefRecords(arg); diff --git a/src/package.json b/src/package.json index f42dbae..124c46b 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-nfc", - "version": "4.0.2", + "version": "4.0.3", "description": "NFC plugin for your NativeScript app", "main": "nfc", "typings": "index.d.ts",