我正在尝试从webview2获取值并将其传递给webview1,但它显示Uncaught Error:错误调用NPObject上的方法。 * getnb()工作,typenb()不工作。我做错了什么?
mwebview=(WebView)findViewById(R.id.webView2);
mwebview.getSettings().setJavaScriptEnabled(true);
mwebview.setWebChromeClient(new WebChromeClient());
mwebview.setWebViewClient(new WebViewClient());
mwebview.loadUrl("http://example.com");
webview2=(WebView)findViewById(R.id.webView3);
webview2.getSettings().setJavaScriptEnabled(true);
webview2.setWebChromeClient(new WebChromeClient());
webview2.addJavascriptInterface(this,"MYOBJECT");
webview2.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view,String url){
super.onPageFinished(view,url);
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.KITKAT) {
webview2.evaluateJavascript("javascript:document.documentElement.appendChild(document.createElement('script')).src='http://example.com/get.js'",null);
}else{
webview2.loadUrl("javascript:document.documentElement.appendChild(document.createElement('script')).src='http://example.com/get.js'");
}
}
});
webview2.loadUrl("https://example.com");
}
@JavascriptInterface
public void getnb(String str){
this.yournumber = str;
Log.d("your number is",yournumber);
typenb();
}
@JavascriptInterface
public void typenb(){
mwebview.loadUrl("javascript:alert('"+ yournumber +"');");
}