- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在 Android Studio 中开发一个应用程序来读写 NFC 标签,特别是 Mifare Classic 标签。 2016 年初(一年前),我设法在我的智能手机(使用 S.O. KitKat)上开发和测试它。
正如我提到的,抛开应用程序,在更新 Android Studio、SDK 和 S.O. 的版本之后。从我的智能手机到 MarshMallow,尝试写入标签时出现此错误:“java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法‘void android.nfc.tech.MifareClassic.connect()’”。
这个错误显然是在尝试连接到 MifareClassic 标签时产生的。
附上我的 Activity 代码,用我认为不相关的...替换了一些部分。
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Bundle;
import android.view.View.OnClickListener;
...
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@SuppressLint("Escribir")
public class escribir extends Activity {
NfcAdapter adapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
boolean writeMode;
Tag myTag;
MifareClassic mfc;
Context context;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datospropietario);
context = this;
...
Button btnWrite = (Button)findViewById(R.id.button);
final String b = getIntent().getExtras().getString("datos");
btnWrite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String mensaje = (b + ...);
if (first.getText().toString().isEmpty()) {
Toast.makeText(context, context.getString(R.string.missing_fields), Toast.LENGTH_SHORT).show();
} else {
if (myTag == null) {
Toast.makeText(context, context.getString(R.string.error_notag), Toast.LENGTH_LONG).show();
} else {
MifareClassic tmpMFC = null;
try {
tmpMFC = MifareClassic.get(myTag);
} catch (Exception e) {
Toast.makeText(context, context.getString(R.string.error_notag), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
mfc = tmpMFC;
int sect;
if (mfc != null) {
sect = mfc.getSectorCount();
}
try {
mfc.connect();
...
} catch (IOException e) {
Toast.makeText(context, context.getString(R.string.error_notag), Toast.LENGTH_LONG).show();
e.printStackTrace();
myTag = null;
}
}
}
});
adapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
writeTagFilters = new IntentFilter[]{tagDetected};
}
private void write(String text, Tag tag, int sector) throws IOException, FormatException {
NdefRecord[] records = {createRecord(text), NdefRecord.createApplicationRecord("my_app")};
NdefMessage mensaje = new NdefMessage(records);
NdefFormatable formatable = NdefFormatable.get(tag);
if (formatable != null) {
formatable.connect();
formatable.format(mensaje);
formatable.close();
} else {
Ndef ndef = Ndef.get(tag);
ndef.connect();
ndef.writeNdefMessage(mensaje);
ndef.close();
}
MifareClassic mfc = MifareClassic.get(tag);
...
}
@SuppressLint("Escribir") private NdefRecord createRecord(String text) throws UnsupportedEncodingException{
String lang = "es";
byte[] textBytes = text.getBytes();
byte[] langBytes = lang.getBytes("US-ASCII");
int langLength = langBytes.length;
int textLength = textBytes.length;
byte[] payLoad = new byte[1 + langLength + textLength];
payLoad[0] = (byte) langLength;
System.arraycopy(langBytes, 0, payLoad, 1, langLength);
System.arraycopy(textBytes, 0, payLoad, 1 + langLength, textLength);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payLoad);
}
@SuppressLint("Escribir") protected void onNewIntent(Intent intent){
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
}
public void onPause(){
super.onPause();
WriteModeOff();
}
public void onResume(){
super.onResume();
WriteModeOn();
}
@SuppressLint("Escribir") private void WriteModeOn(){
writeMode = true;
adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
}
@SuppressLint("Escribir") private void WriteModeOff(){
writeMode = false;
adapter.disableForegroundDispatch(this);
}
}
最佳答案
在互联网上研究了这个问题后,我发现 NFC 标签写入问题出现在各种设备上,例如某些 HTC 或 Sony Xperia 型号。这些问题是在Android版本升级到SDK 5.1(Lollipop)后出现的。
这个问题的起因是一些制造商修改了堆栈顺序,发现不同的标签类型在请求 TechExtras 时导致冲突并返回 SAK 空值或错误值。
我找到的详细解释如下:
HTC One:看来,这个bug的原因是NfcA的TechExtras为空。但是,TechList 包含 MifareClassic。
Sony Xperia Z3(+ 仿真 MIFARE Classic 标签): buggy 标签在 TechList 中有两个具有不同 SAK 值的 NfcA 和一个 MifareClassic(带有第二个 NfcA 的 Extra)。第二个 NfcA 和 MifareClassic 技术都具有 0x20 的 SAK。根据恩智浦关于识别 MIFARE 标签的指南(第 11 页),这是一个 MIFARE Plus 或 MIFARE DESFire 标签。此方法使用两个 NfcA 出现的 SAK 值或运算(如 NXP 的 MIFARE 类型识别过程指南中所述)创建一个新的 extra,并将第一个 NfcA 的 Extra 替换为新的。
有关更多信息,请参阅 https://github.com/ikarus23/MifareClassicTool/issues/52
以及解决我问题的 bildin 用户提出的补丁:
public Tag patchTag(Tag oTag)
{
if (oTag == null)
return null;
String[] sTechList = oTag.getTechList();
Parcel oParcel, nParcel;
oParcel = Parcel.obtain();
oTag.writeToParcel(oParcel, 0);
oParcel.setDataPosition(0);
int len = oParcel.readInt();
byte[] id = null;
if (len >= 0)
{
id = new byte[len];
oParcel.readByteArray(id);
}
int[] oTechList = new int[oParcel.readInt()];
oParcel.readIntArray(oTechList);
Bundle[] oTechExtras = oParcel.createTypedArray(Bundle.CREATOR);
int serviceHandle = oParcel.readInt();
int isMock = oParcel.readInt();
IBinder tagService;
if (isMock == 0)
{
tagService = oParcel.readStrongBinder();
}
else
{
tagService = null;
}
oParcel.recycle();
int nfca_idx=-1;
int mc_idx=-1;
for(int idx = 0; idx < sTechList.length; idx++)
{
if(sTechList[idx] == NfcA.class.getName())
{
nfca_idx = idx;
}
else if(sTechList[idx] == MifareClassic.class.getName())
{
mc_idx = idx;
}
}
if(nfca_idx>=0&&mc_idx>=0&&oTechExtras[mc_idx]==null)
{
oTechExtras[mc_idx] = oTechExtras[nfca_idx];
}
else
{
return oTag;
}
nParcel = Parcel.obtain();
nParcel.writeInt(id.length);
nParcel.writeByteArray(id);
nParcel.writeInt(oTechList.length);
nParcel.writeIntArray(oTechList);
nParcel.writeTypedArray(oTechExtras,0);
nParcel.writeInt(serviceHandle);
nParcel.writeInt(isMock);
if(isMock==0)
{
nParcel.writeStrongBinder(tagService);
}
nParcel.setDataPosition(0);
Tag nTag = Tag.CREATOR.createFromParcel(nParcel);
nParcel.recycle();
return nTag;
}
此补丁由 bildin ( https://github.com/bildin) 提供。
虽然我测试的设备不是早期品牌,但该补丁在我的 Moto X(第一代)上运行良好,带有修改后的 ROM Marshmallow,所以我想它也适用于具有NXP芯片PN544
关于android - 尝试在空对象引用上调用虚拟方法 'void android.nfc.tech.MifareClassic.connect()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42963329/
我正在尝试使用 NFC 从血糖仪读取数据,目前在 Android 上,还没有尝试过 iOS(没有带 NFC 的手机)。 我正在使用 react-native-nfc-manager 作为库和它附带的示
我的 friend 要我帮助他为PIC12 MCU编写一个小程序。我们想要 当输入电压在2秒钟内小于1.9V时,程序停止运行。 当输入电压超过2.5V时,程序将立即作出 react 。 我尝试通过阅读
我想在 Android 上编写我的第一个 NFC 应用程序。为此,我使用 android 开发人员链接:http://developer.android.com/guide/topics/nfc/in
我有一个开发人员已将解锁 Lumia 635 昨天更新为Windows 10 Mobile Technical Preview(操作系统版本: 10.0.12562.84 )。 在Visual Stu
我正在尝试使用 BigVideo.js,它在 Chrome 和 Safari 中运行良好,但在 Firefox 中显示错误“this.tech is undefined”导致视频无法运行。有没有人以前
我在使用 Video.JS 脚本(结合 BigVideo.js)时遇到一点问题 第 2511 行:this.tech.isReady 为空或不是对象 以下情况发生在 IE7-8 和 Firefox 1
本文整理了Java中pl.allegro.tech.hermes.infrastructure.zookeeper.ZookeeperPaths类的一些代码示例,展示了ZookeeperPaths类的
本文整理了Java中pl.allegro.tech.hermes.common.broker.ZookeeperBrokerStorage类的一些代码示例,展示了ZookeeperBrokerStor
本文整理了Java中pl.allegro.tech.hermes.infrastructure.zookeeper.ZookeeperTopicRepository类的一些代码示例,展示了Zookee
本文整理了Java中pl.allegro.tech.hermes.infrastructure.zookeeper.ZookeeperSubscriptionRepository类的一些代码示例,展示
本文整理了Java中pl.allegro.tech.hermes.infrastructure.zookeeper.ZookeeperGroupRepository类的一些代码示例,展示了Zookee
Elasticsearch是否支持tokenizer来按大写字符对术语进行标记,例如:将TheStarTech标记为术语[The, Star, Tech]。 Pattern tokenizer似乎有帮
本文整理了Java中pl.allegro.tech.hermes.test.helper.zookeeper.ZookeeperWaiter类的一些代码示例,展示了ZookeeperWaiter类的具
本文整理了Java中pl.allegro.tech.hermes.common.admin.zookeeper.ZookeeperAdminCache类的一些代码示例,展示了ZookeeperAdmi
本文整理了Java中pl.allegro.tech.hermes.common.message.undelivered.ZookeeperUndeliveredMessageLog类的一些代码示例,展
不知道为什么 migrate 正在寻找 'teches' 而不是真正的表名 'techs'? 文件:TechsTableSeeder.php class TechsTableSeeder extend
这个问题在这里已经有了答案: C/C++: Pointer Arithmetic (7 个答案) Why does int pointer '++' increment by 4 rather th
我在我的网站 roundabouttech.com 上做了 ssl。但问题是当我输入完整的“https://roundabouttech.com”时,它会以 https 打开。但是当我只输入“roun
本文整理了Java中pl.allegro.tech.hermes.infrastructure.zookeeper.ZookeeperPaths.consumersRateHistoryPath()方
本文整理了Java中pl.allegro.tech.hermes.infrastructure.zookeeper.ZookeeperPaths.consumersMaxRatePath()方法的一些
我是一名优秀的程序员,十分优秀!