【问题标题】:Android WebView Javascript communication errorAndroid WebView Javascript 通讯错误
【发布时间】:2015-07-13 09:46:14
【问题描述】:

我的 WebView 应用程序有问题。我将通过 Javascript 开始 WebView 和 App 之间的通信。 错误:

E/Web 控制台:未捕获的错误:在 NPObject 上调用方法时出错。

Javascript接口:

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

WebView 设置:

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

谁能帮忙?

【问题讨论】:

    标签: javascript java android webview


    【解决方案1】:

    您是否尝试在 UI 线程上的 WebAppInterface.launch() 和 WebAppInterface.test() 中运行您的代码:link

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      相关资源
      最近更新 更多