Android NFC,在onCreate 中进行空检查吗?
我有代码来初始化 NFC 适配器,但我不确定如何在 onCreate 期间初始化变量。无论 NFC 标签是否在附近,应用程序都可以初始化。如果有人只是打开应用程序。因此,当我到达此行 NfcV nfcMessage = NfcV.get(new TagGet().getTag());
时,它会崩溃,因为它为空,如果您只是加载应用程序,则那里没有标签靠你自己。我如何在此处检查 != null
我不确定要检查哪一部分或如何构建此代码。
结果是我想读取标签的内容(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();
I have code to initialize my NFC adapter, but I am not sure how to intialize the variables during the onCreate. The app can be initialized whether an NFC TAG is in proximity or not, ie. if someone simply opened the app. So when I get down to this line NfcV nfcMessage = NfcV.get(new TagGet().getTag());
it crashes because it is null, there is no tag there if you just load the app on your own. How do I check here for != null
I'm not sure which part to check or how to structure this code.
The result is that I want to read the contents of the tag IFF a tag is present. Otherwise just load the layout and wait for a NfcV tag to be scanned.
// 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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Hear 是我制作第一个 Nfc 应用程序的基本方式的代码大纲。我尝试替换值(我使用 nfcA 而不是 nfcV),以便您可以更好地阅读它。这是一个非常基本的结构,您需要用自己的代码填充各个部分,但它应该让您很好地了解 NFC 应用程序的结构如何工作。
}
Hear is a code outline of the basic way I made my first Nfc app. I tried to replace values (I used nfcA instead of nfcV) so you could read it better. It is a very basic structure, and you would need to fill in parts with your own code, but It should give you a good idea of how the structure of an NFC app can work.
}