我有代碼來初始化我的NFC適配器,但我不知道如何在onCreate期間初始化變量。無論NFC標籤是否接近,該應用都可以初始化,即。如果有人只是打開應用程序。所以,當我回到這一行NfcV nfcMessage = NfcV.get(new TagGet().getTag());
它崩潰,因爲它是空的,如果你只是自己加載應用程序,那裏沒有標籤。我如何在這裏檢查!= null
我不確定要檢查哪個部分或如何構造此代碼。Android NFC,在onCreate做空檢查?
結果是我想讀標籤存在的標籤IFF的內容。否則,只需加載佈局並等待NfcV標籤進行掃描。
// Setup an intent filter for all MIME based dispatches
IntentFilter nfcv = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
try {
nfcv.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[] {
nfcv,
};
// Setup a tech list for all NfcF tags
mTechLists = new String[][] { new String[] { NfcV.class.getName() } };
//mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
NfcV nfcMessage = NfcV.get(new TagGet().getTag());
byte[] data = new byte[2048]; //tag length can't be any larger
String value = "";
try {
data = nfcMessage.transceive(new byte[2048]);
value = new String(data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
你做的方法 「公共無效onNewIntent(意向意圖)」 在周圍的類?這樣當你的應用程序運行並且在NewIntent運行時發現標籤時。然後你可以讓你的標籤知道標籤在那裏。然後,您需要啓動另一個線程來處理您的應用和標籤之間的通信。你雖然朝着正確的方向前進! –