在这里,我正在开发一个将读取 NFC 标签中的记录的安卓应用程序。这是我的程序中读取标签的代码部分,当它读取时,会发生越界异常。
NdefMessage[] msgs = null;
String action = intent.getAction();
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)||
NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)){
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if(rawMsgs != null){
msgs = new NdefMessage[rawMsgs.length];
for(int i=0; i<rawMsgs.length; i++){
msgs[i] = (NdefMessage) rawMsgs[i];
}
}else{
byte[] empty = new byte[]{};
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
msgs = new NdefMessage[]{msg};
}
}
if(msgs==null){
String[] array = new String[]{"No Tag discovered!"};
return array;
}else{
String[] array = new String[]{new String(msgs[0].getRecords()[0].getPayload()),
new String(msgs[1].getRecords()[0].getPayload())
};
return array;
}
当我隐藏这句“new String(msgs 1 .getRecords()[0].getPayload())”时,我的程序可以顺利运行,所以我很确定程序在这一行。请问我该如何解决这个问题?