1
votes

I'm stuck on my project and requesting your help!
I'm trying to call an Activity(BarcodeScannerActivity.java) from webview in MainActivity.java with button click using javascript call. I think I messed up with parameters somewhere... I'm not sure where I've made mistake, I just copied and modified the codes from blogs and stackoverflow.

I'm having this error in my Android Studio logcat:

[INFO:CONSOLE(315)] "Uncaught Error: Error calling method on NPObject.", source: http:// .........

this is my server side html:

 <input type="button" value="Scanner" id="BtnScan" class="btn_dark" onclick="callActivity();" /> 
<script>
  function callActivity() {
    Android.openBarcodeScanner();
  }
</script>

this is my MainActivity.java containing webview:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().hide();

        //getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        //getActionBar().hide();

        setContentView(R.layout.activity_main);

        MainWebView = (WebView) findViewById(R.id.mainWebview);
        MainWebView.setWebViewClient(new WebViewClient());

        //-- add javascript listener
        MainWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
        MainWebView.getSettings().setJavaScriptEnabled(true);

        MainWebView.loadUrl(WebViewURL);
    }

..skipped for brevity..

public class WebAppInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void openBarcodeScanner(String str) {
            Intent barcodeScanIntent = new Intent(mContext, BarcodeScannerActivity.class);
            mContext.startActivity(barcodeScanIntent);
        }
}

and last, the BarcodeScannerActivity.java that wants to be called:

public class BarcodeScannerActivity extends Activity implements View.OnClickListener {

//    private static final  R = ;
    // use a compound button so either checkbox or switch widgets work.
    private CompoundButton autoFocus;
    private CompoundButton useFlash;
    private TextView statusMessage;
    private TextView barcodeValue;

    private static final int RC_BARCODE_CAPTURE = 9001;
    private static final String TAG = "BarcodeMain";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.barcode_scanner);

        statusMessage = (TextView)findViewById(R.id.status_message);
        barcodeValue = (TextView)findViewById(R.id.barcode_value);

        autoFocus = (CompoundButton) findViewById(R.id.auto_focus);
        useFlash = (CompoundButton) findViewById(R.id.use_flash);

        findViewById(R.id.read_barcode).setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.read_barcode) {
            // launch barcode activity.
            Intent intent = new Intent(this, BarcodeCaptureActivity.class);
            intent.putExtra(BarcodeCaptureActivity.AutoFocus, autoFocus.isChecked());
            intent.putExtra(BarcodeCaptureActivity.UseFlash, useFlash.isChecked());

            startActivityForResult(intent, RC_BARCODE_CAPTURE);
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_BARCODE_CAPTURE) {
            if (resultCode == CommonStatusCodes.SUCCESS) {
                if (data != null) {
                    Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
                    statusMessage.setText(R.string.barcode_success);
                    barcodeValue.setText(barcode.displayValue);
                    Log.d(TAG, "Barcode read: " + barcode.displayValue);
                } else {
                    statusMessage.setText(R.string.barcode_failure);
                    Log.d(TAG, "No barcode captured, intent data is null");
                }
            } else {
                statusMessage.setText(String.format(getString(R.string.barcode_error),
                        CommonStatusCodes.getStatusCodeString(resultCode)));
            }
        }
        else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

and one last question is, would this javascript code work in my situation, too? or do I need to modify a bit?

function  callActivity(){

            if(window.android){

                console.info("android");

                 window.android.openBarcodeScanner(number.value);           

            }

            else{

                console.info("web");

                document.form.submit();

            }

        }

    }

Thank you so much in advance!

1

1 Answers

1
votes

Your interface takes a string as a parameter:

@JavascriptInterface
public void openBarcodeScanner(String str) {

But your invocation doesn't pass any parameters:

Android.openBarcodeScanner();