java - 我的应用程序在 onNewintent() 执行时崩溃
问题描述
我只想创建一个同时实现指纹扫描和 NFC 读取的 android 应用程序。它适用于不同的时间。但是如果我在指纹扫描仪打开时尝试扫描 NFC 卡,它就会崩溃。NFC 卡正在读取,Android 设备将生成一个新的 Intent。此时将调用 onNewIntent() 方法。在此 onNewIntent() 之后,控件将转到 onResume()。在 onResume() 中添加了指纹扫描代码。当控制到这里时,应用程序会崩溃。
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
initDevice();
if (mFingerprint != null) {
tvInfo.setText("");
mFingerprint.startIdentification();
}
WriteModeOn();
}
public void initDevice() {
new InitTask().execute();
}
public class InitTask extends AsyncTask<String, Integer, Boolean> {
ProgressDialog mypDialog;
@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mFingerprint.init();
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
mypDialog.dismiss();
if (!result) {
// Toast.makeText(Read_RFIDActivity.this, "fail",
// Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Read_RFIDActivity.this, "initialization success", Toast.LENGTH_SHORT).show();
}
tvInfo.setText("");
mFingerprint.startIdentification();
// start();
getEmpRealm();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mypDialog = new ProgressDialog(Read_RFIDActivity.this);
mypDialog.setCanceledOnTouchOutside(false);
mypDialog = ProgressDialog.show(Read_RFIDActivity.this, "Scanner Initializing...", "Please wait...", true, false);
}
}
日志猫
2020-02-24 15:46:36.523 19339-19339/com.oges.timefingerprint.attendance V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@54870d
2020-02-24 15:46:36.523 19339-19339/com.oges.timefingerprint.attendance V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@631a6c2
2020-02-24 15:46:36.526 19339-19339/com.oges.timefingerprint.attendance V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@74e3010
2020-02-24 15:46:36.526 19339-19339/com.oges.timefingerprint.attendance V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@b4f8709
2020-02-24 15:46:36.537 19339-19339/com.oges.timefingerprint.attendance I/DeviceAPI_Fingerprint: startIdentify =>runing=true
2020-02-24 15:46:36.537 19339-19339/com.oges.timefingerprint.attendance I/DeviceAPI_Fingerprint: what==iENROLLL && enrollCallBack==null
2020-02-24 15:46:36.604 19339-19435/com.oges.timefingerprint.attendance I/DeviceAPI_Fingerprint: morphoIdentify() end
2020-02-24 15:46:36.605 19339-19435/com.oges.timefingerprint.attendance E/AndroidRuntime: FATAL EXCEPTION: Thread-50
Process: com.oges.timefingerprint.attendance, PID: 19339
java.lang.NullPointerException: Attempt to get length of null array
at java.lang.StringFactory.newStringFromChars(StringFactory.java:211)
at com.rscja.deviceapi.FingerprintWithMorpho.morphoIdentify(FingerprintWithMorpho.java:917)
at com.rscja.deviceapi.FingerprintWithMorpho.access$19(FingerprintWithMorpho.java:897)
at com.rscja.deviceapi.FingerprintWithMorpho$ThreadIdentification.getData(FingerprintWithMorpho.java:735)
at com.rscja.deviceapi.FingerprintWithMorpho$ThreadIdentification.run(FingerprintWithMorpho.java:724)
2020-02-24 15:46:36.610 19339-19339/com.oges.timefingerprint.attendance D/DeviceAPI: pre finger init
2020-02-24 15:46:36.611 19339-19339/com.oges.timefingerprint.attendance E/DeviceAPI_Fingerprint: free() err:-1
2020-02-24 15:46:36.611 19339-19339/com.oges.timefingerprint.attendance E/freeeeeeeeeee: ------In Free()........
2020-02-24 15:46:36.731 19339-19339/com.oges.timefingerprint.attendance V/AsyncHttpRH: Progress 13 from 13 (100%)
2020-02-24 15:46:36.732 19339-19339/com.oges.timefingerprint.attendance I/Server Request:: {"code":1}
解决方案
试试这个我的工作代码。
首先将以下行添加到 onCreate()
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
然后在您的调用活动 onResume() 方法上添加以下行
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
IntentFilter[] nfcIntentFilter = new IntentFilter[]{techDetected, tagDetected, ndefDetected};
if (mNfcAdapter != null)
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, nfcIntentFilter, null);
然后将下面的代码行添加到 onPause()
if (mNfcAdapter != null)
mNfcAdapter.disableForegroundDispatch(this);
并在清单中添加以下权限
<uses-permission android:name="android.permission.NFC" />
推荐阅读
- flutter - 如何导航到具有构造函数参数的页面?
- reactjs - 尝试从一个对象渲染多个属性到单个 JSX 元素 - React 组件
- mongodb - 已连接到 MLab,但不会连接到 localhost
- python - 计算列表中的连续出现和
- mysql - mysqldump:无法执行 'SHOW PACKAGE STATUS WHERE Db = 当 rake db:migrate 完成迁移时
- python-3.x - Python Selenium:如何优化按钮选择和提交过程?
- java - 将多个分隔符传递给 StringTokenizer 构造函数
- python - 有没有一种有效的方法可以从熊猫的散点数据中创建颜色网格?
- sql - 根据列的值使用 SQL 求和
- javascript - 向 JS 多维对象添加新数据