使用@JavascriptInterface从webview调用活动?

时间:2016-03-07 02:59:24

标签: javascript java android android-studio webview

我坚持我的项目并请求你的帮助! 我正在尝试从MainActivity.java中的webview调用一个Activity(BarcodeScannerActivity.java),点击按钮使用javascript调用。我觉得我搞砸了某些地方的参数......我不确定我在哪里弄错了,我只是复制并修改了博客和stackoverflow中的代码。

我的Android Studio logcat中出现此错误:

  

[INFO:CONSOLE(315)]“未捕获错误:在NPObject上调用方法时出错。”,来源:http:// .........

这是我的服务器端html:

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

这是我的包含webview的MainActivity.java:

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

最后,想要被调用的BarcodeScannerActivity.java:

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

提前非常感谢你!

0 个答案:

没有答案