简体   繁体   中英

Android Activity finish method kills application

I'm trying to build an android application that uses barcode reader.

The app workflow is as follow:

  1. The application starts with "MainActivity" displayed
  2. User clicks on a button in MainActivity and starts a second activity for barcode scan
  3. The user scans the barcode, and the result is displayed in a text field in MainActivity

The main activity starts the barcode scan activity with "startActivityForResult", and listen to result events.

The barcode scan activity listen on barcode scan library's scan event and sets the result with "setResult" method.

The barcode scan activity closes itself and let the main activity read the value.

All of the above works as expected, but after the onActivityResult event handling, the application terminates.

In log file i can find only a generic app-kill message

Below you can find code snippets and log out.

Main Activity.java - Button click and onActivityResult:

b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setClass(getBaseContext(), BarcodeScannerActivity.class);

        startActivityForResult(i, 100);
    }
});

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i("BARCODE_RESULT", data.getStringExtra("displayValue"));
    super.onActivityResult(requestCode, resultCode, data);

    TextView t = (TextView) findViewById(R.id.textView);
    t.setText(data.getStringExtra("displayValue"));

    Log.i("BARCODE_RESULT", data.getStringExtra("displayValue"));
}

Barcode Activity - OnBarcodeScan

public void onScanned(final Barcode barcode) {
    Log.i("BARCODE", "Barcode scanned: " + barcode.displayValue);

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getBaseContext(), "Barcode: " + barcode.displayValue, Toast.LENGTH_LONG).show();

            Intent intent=new Intent();
            intent.putExtra("displayValue",barcode.displayValue);
            setResult(Activity.RESULT_OK, intent);

            finish();
        }
    });
}

And, last but not least, the logcat out:

/** I/BARCODE: Barcode scanned: 8005200010448
/** I/BARCODE_RESULT: 8005200010448
/** I/BARCODE_RESULT: 8005200010448
/** I/Process: Sending signal. PID: 10012 SIG: 9

* EDIT * I have just found the exception below in device's log, it has been thrown right before the SIG: 9 log

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.huawei.lcagent.client.LogCollectManager.getUserType()' on a null object reference
W/System.err:     at com.android.server.util.ReportTools.getUserType(ReportTools.java:86)
W/System.err:     at com.android.server.util.ReportTools.isBetaUser(ReportTools.java:73)
W/System.err:     at com.android.server.util.ReportTools.report(ReportTools.java:58)
W/System.err:     at com.android.server.util.HwUserBehaviourRecord.appExitRecordInnerImpl(HwUserBehaviourRecord.java:125)
W/System.err:     at com.android.server.util.HwUserBehaviourRecord.access$200(HwUserBehaviourRecord.java:32)
W/System.err:     at com.android.server.util.HwUserBehaviourRecord$AsyUploadLooperThread$1.handleMessage(HwUserBehaviourRecord.java:255)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:150)
W/System.err:     at com.android.server.util.HwUserBehaviourRecord$AsyUploadLooperThread.run(HwUserBehaviourRecord.java:267)

Thanks in advance for the support

Mattia

Try removing runOnUiThread() over your finish() method. I don't think there is any need of runOnUiThread() if this is all code you have in onScan() .

public void onScanned(final Barcode barcode) {
    Log.i("BARCODE", "Barcode scanned: " + barcode.displayValue)
            Toast.makeText(getBaseContext(), "Barcode: " + barcode.displayValue, Toast.LENGTH_LONG).show();

            Intent intent=new Intent();
            intent.putExtra("displayValue",barcode.displayValue);
            setResult(Activity.RESULT_OK, intent);

            finish();
}

Also, on your device check if all permissions are granted in settings.

Try to use YourActivity.this.finish instead of finish.

Your_Activity.this.finish();

I hope this works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM