0
我的WebView應用程序出現問題。我將通過Javascript啓動WebView和App之間的通信。 的錯誤:Android WebView Javascript通信錯誤
E/Web Console﹕ Uncaught Error: Error calling method on NPObject.
的JavascriptInterfaces:
public class MainActivity extends Activity {
public class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void launch(String name) {
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(name);
startActivity(LaunchIntent);
}
@JavascriptInterface
public void test() {
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("javascript: alert('test');");
}
}
web視圖設置:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setInitialScale(100);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setWebViewClient(new WebViewClient());
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
myWebView.requestFocus(View.FOCUS_DOWN);
myWebView.loadUrl("file:///android_asset/index.html");
}
任何人誰可以幫助嗎?