java – My app crashes when onNewintent() executes-ThrowExceptions

Exception or error:

I just want to create an android application that implements both Fingerprint scanning and NFC reading together. It works well in at different times. But if I try to scan The NFC card when the fingerprint scanner is ON, it gets crashes.
The NFC card is reading, a new intent will generated by the android device.The onNewIntent() method will invoke at that time. After this onNewIntent(), the control will goes to onResume(). Fingerprint scanning codes are added in onResume().When control goes here, the app get crashing.

 @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);

        }

    }

Logcat

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}
How to solve:

try this my working code.

first add below line into onCreate()

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

then Add below line on your calling activity onResume() method

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);

then add below line of code into onPause()

 if (mNfcAdapter != null)
        mNfcAdapter.disableForegroundDispatch(this);

and into manifest add below permission

<uses-permission android:name="android.permission.NFC" />

Leave a Reply

Your email address will not be published. Required fields are marked *