android - 此代码在 android 2.2 中正常工作但不适用于 android 2.3 及更高版本

标签 android android-webview

WebView browser;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        browser = (WebView) findViewById(R.id.webve);


        browser.getSettings().setJavaScriptEnabled(true);
        browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
        browser.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url)
            {
                browser.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");

            }

        });


        browser.loadUrl("http://www.google.com");
    }

    final Context myApp = this;
    Builder builder;

    String content_html;

    class MyJavaScriptInterface
    {
        @SuppressWarnings("unused")
        public void showHTML(String html)
        {
           builder = new AlertDialog.Builder(myApp);
                builder.setTitle("HTML")
                .setMessage(html)
                .setPositiveButton(android.R.string.ok, null)
            .setCancelable(false)
            .create()
            .show();
                content_html = html;
                Log.i("html", html+" ");
        }

    }

此代码在 android 2.2 中正常工作,但在 android 2.3 及更高版本中无法正常工作...给出下面显示的错误消息..

12-19 14:53:33.786: W/dalvikvm(452): JNI WARNING: jarray 0x40543998 points to non-array object (Ljava/lang/String;)
    12-19 14:53:33.831: I/dalvikvm(452): "WebViewCoreThread" prio=5 tid=9 NATIVE
    12-19 14:53:33.838: I/dalvikvm(452):   | group="main" sCount=0 dsCount=0 obj=0x4051e198 self=0x29b650
    12-19 14:53:33.838: I/dalvikvm(452):   | sysTid=460 nice=0 sched=0/0 cgrp=default handle=2733960
    12-19 14:53:33.858: I/dalvikvm(452):   | schedstat=( 2873090555 2669702962 182 )
    12-19 14:53:33.878: I/dalvikvm(452):   at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
    12-19 14:53:33.885: I/dalvikvm(452):   at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
    12-19 14:53:33.905: I/dalvikvm(452):   at android.webkit.BrowserFrame.loadUrl(BrowserFrame.java:246)
    12-19 14:53:33.924: I/dalvikvm(452):   at android.webkit.WebViewCore.loadUrl(WebViewCore.java:1570)
    12-19 14:53:33.924: I/dalvikvm(452):   at android.webkit.WebViewCore.access$1400(WebViewCore.java:53)
    12-19 14:53:33.934: I/dalvikvm(452):   at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:956)
    12-19 14:53:33.955: I/dalvikvm(452):   at android.os.Handler.dispatchMessage(Handler.java:99)
    12-19 14:53:33.955: I/dalvikvm(452):   at android.os.Looper.loop(Looper.java:130)
    12-19 14:53:33.955: I/dalvikvm(452):   at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:629)
    12-19 14:53:33.955: I/dalvikvm(452):   at java.lang.Thread.run(Thread.java:1019)
    12-19 14:53:33.964: E/dalvikvm(452): VM aborting

最佳答案

Jason Shah 在 this post 中解释了一切:

Handling Android 2.3 WebView's broken AddJavascriptInterface

(Apologies in advance to my normal readers for this technical topic.)

The Google Android team released the Android 2.3 ("Gingerbread") SDK two days ago, to much fanfare. This has sent the tech blogging world into a publishing frenzy, as it usually does. However, a potentially disastrous bug has surfaced that could crash literally thousands of apps in the Android Market immediately after opening the app.

The problem is described succintly here: http://code.google.com/p/android/issues/detail?id=12987 In short: Many apps show all or part of their UI with embedded WebViews that can render HTML. Those WebViews make use of a great feature that bridges JavaScript (in the HTML) to the native Java code that "surrounds" the WebView. This bridge is completely broken in Android 2.3. Trying to make even a basic call breaks the WebView immediately and crashes the app.

I believe members of the Android team are aware of the problem, and from early reports, it does not affect the Nexus S (the first Android 2.3 phone). This doesn't really help those of us working against the emulator, however.

关于android - 此代码在 android 2.2 中正常工作但不适用于 android 2.3 及更高版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8563403/

相关文章:

android - 在视频上添加文本时出现ffmpeg错误

Android - WebView 中来自 android_assets 的 html,CSS 未在 ICS 中加载

android - 更改方向时如何在 fragment 中保存 WebView 状态?

android - setJavaScriptEnabled(true) 的替代解决方案;

Android从外部存储读取html文件

android - 我在哪里可以从 Android 设备读取 WiFi 数据包丢失计数器?

android - Gradle无法与Google map 同步

android - 如何使用 fragment 为 "scrolling view"的协调器布局

android - 将条形码转换为产品名称

android - Javascript 在 Android webview 中不起作用