1

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

Mattiag
  • 108
  • 2
  • 10
  • Are you using android:noHistory="true" in your manifest ? – Sachin Aggarwal Aug 31 '17 at 13:10
  • Why are you putting your code inside runOnUIThread() in BarcodeActvity ? I think this is causing the problem ! – Sachin Aggarwal Aug 31 '17 at 13:17
  • Hi Sachin, i don't have that parameter, I'm currently looking into it. Please view my edited question. Is that error possibily related to this setting? Thanks, Mattia ** EDIT ** The barcode library sends me responses in async thread, so I added the runOnUiThread because of that – Mattiag Aug 31 '17 at 13:18
  • [Here](https://stackoverflow.com/questions/39588023/unable-to-start-android-app-due-to-logcollectmanager-getusertype-error) is a similar error. It is unclear whether or not the poster resolved the issue, but he/she seems to think it has something to do with code in the [Application.onTrimMemory()](https://developer.android.com/reference/android/app/Application.html#onTrimMemory(int)) method. Are you implementing that method? Or the [corresponding method](https://developer.android.com/reference/android/app/Activity.html#onTrimMemory(int)) in your `Activity`? – Bryan Aug 31 '17 at 13:30
  • Looks the exception log is not relevant to your problem. Can you upload the apk somewhere and let me run it on my device? – cmoaciopm Aug 31 '17 at 14:07

2 Answers2

0

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.

Sachin Aggarwal
  • 1,095
  • 8
  • 17
  • Hi Sachin, as I wrote in my question's comments, the barcode library i'm using notify for scan completes on async thread, that's why i put the runOnUIThread. If i remove that the activity doesn't close and the toast isn't showed. – Mattiag Aug 31 '17 at 13:29
  • Thanks, I didn't saw your edit. Let me look into this, thanks :) – Sachin Aggarwal Aug 31 '17 at 13:35
-1

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

Your_Activity.this.finish();

I hope this works!

T. Maciej
  • 9
  • 4
  • 1
    Since the `Runnable` interface doesn't have a `finish()` method, the call to `finish()` is referencing the `Activity` method. Therefore there is no need to qualify the method with `MainActivity.this.finish()`, as it is the same as calling `finish()` in this case. – Bryan Aug 31 '17 at 14:06