gpt4 book ai didi

android - 尝试在空对象引用上调用虚拟方法 'void android.nfc.tech.MifareClassic.connect()'

转载 作者:行者123 更新时间:2023-11-30 00:38:34 25 4
gpt4 key购买 nike

我一直在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com