开发者

When I invoke a Java function from my Javascript code, WebView crashes. Why?

This is probably an easy question. When I invoke a Java function from my Javascript code, WebView crashes. Why? This code is straight from the docs.

Java:

public class HelloWebView extends Activity {

    private WebView mWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        mWebView.loadUrl("file:///android_asset/myPage.html");
    }

    public class JavaScriptInterface {
        Context mContext;

        JavaScriptInterface(Context c) {
            mContext = c;
        }

        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }
}

HTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html>
<head>
    <script type="text/javascript">
        function showAndroidToast(toast) {
            Android.showToast(toast);
        }
    </script>

</head>
<body>
    <input type="button" value="Say hello" 
        onClick="showAndroidToast('Hello Android!')" />
</body>

I'm sure I'm just missing something absolutely basic, because I haven't found anyone else with this problem.

By the way, I'm using Android API 10, Gingerbread 2.3.3, and running it on an emulator. Everything loads and works fine. I just run into problems when I click the button which crashes WebView and my entire application.

What am I not understanding here?

Logcat:

09-01 09:49:44.955: INFO/System.out(366): debugger has settled (1459)
09-01 09:49:46.696: INFO/ActivityManager(60): Displayed com.hello/.HelloWebView: +5s498ms
09-01 09:49:49.506: WARN/dalvikvm(366): JNI WARNING: jarray 0x40542240 points to non-array object (Ljava/lang/String;)
09-01 09:49:49.526: INFO/dalvikvm(366): "WebViewCoreThread" prio=5 tid=9 NATIVE
09-01 09:49:49.526: INFO/dalvikvm(366):   | group="main" sCount=0 dsCount=0 obj=0x4051db40 self=0x29e2c0
09-01 09:49:49.536: INFO/dalvikvm(366):   | sysTid=375 nice=0 sched=0/0 cgrp=default handle=2745336
09-01 09:49:49.536: INFO/dalvikvm(366):   | schedstat=( 765703801 1439331689 114 )
09-01 09:49:49.536: INFO/dalvikvm(366):   at android.webkit.WebViewCore.nativeTouchUp(Native Method)
09-01 09:49:49.546: INFO/dalvikvm(366):   at android.webkit.WebViewCore.nativeTouchUp(Native Method)
09-01 09:49:49.546: INFO/dalvikvm(366):   at android.webkit.WebViewCore.access$3300(WebViewCore.java:53)
09-01 09:49:49.546: INFO/dalvikvm(366):   at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1158)
09-01 09:49:49.546: INFO/dalvikvm(366):   at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 09:49:49.546: INFO/dalvikvm(366):   at android.os.Looper.loop(Looper.java:123)
09-01 09:49:49.546: INFO/dalvikvm(366):   at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:629)
09-01 09:49:49.556: INFO/dalvikvm(366):   at java.lang.Thread.run(Thread.java:1019)
09-01 09:49:49.616: ERROR/dalvikvm(366): VM aborting
09-01 09:49:49.726: INFO/DEBUG(30): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
09-01 09:49:49.726: INFO/DEBUG(30): Build fingerprint: 'generic/sdk/generic:2.3.3/GRI34/101070:eng/test-keys'
09-01 09:49:49.726: INFO/DEBUG(30): pid: 366, tid: 375  >>> com.hello <<<
09-01 09:49:49.726: INFO/DEBUG(30): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadd00d
09-01 09:49:49.726: INFO/DEBUG(30):  r0 fffffec4  r1 deadd00d  r2 00000026  r3 00000000
09-01 09:49:49.726: INFO/DEBUG(30):  r4 800a45c0  r5 40542240  r6 80085acc  r7 0029ecf0
09-01 09:49:49.726: INFO/DEBUG(30):  r8 44091afc  r9 43f2fe4c  10 00000000  fp 41a4e2e0
09-01 09:49:49.726: INFO/DEBUG(30):  ip 800a4720  sp 440912f0  lr afd19375  pc 80045a4a  cpsr 20000030
09-01 09:49:49.925: INFO/DEBUG(30):          #00  pc 00045a4a  /system/lib/libdvm.so
09-01 09:49:49.925: INFO/DEBUG(30):          #01  pc 00037748  /system/lib/libdvm.so
09-01 09:49:49.925: INFO/DEBUG(30):          #02  pc 00039a10  /system/lib/libdvm.so
09-01 09:49:49.935: INFO/DEBUG(30):          #03  pc 0003a4ec  /system/lib/libdvm.so
09-01 09:49:49.935: INFO/DEBUG(30):          #04  pc 002989d6  /system/lib/libwebcore.so
09-01 09:49:49.945: INFO/DEBUG(30):          #05  pc 002120a4  /system/lib/libwebcore.so
09-01 09:49:49.945: INFO/DEBUG(30):          #06  pc 00113216  /system/lib/libwebcore.so
09-01 09:49:49.945: INFO/DEBUG(30):          #07  pc 00212b70  /system/lib/libwebcore.so
09-01 09:49:49.955: INFO/DEBUG(30):          #08  pc 002c716e  /system/lib/libwebcore.so
09-01 09:49:49.955: INFO/DEBUG(30):          #09  pc 002ca4c6  /system/lib/libwebcore.so
09-01 09:49:49.965: INFO/DEBUG(30):          #10  pc 002d83b0  /system/lib/libwebcore.so
09-01 09:49:49.965: INFO/DEBUG(30):          #11  pc 002cfae8  /system/lib/libwebcore.so
09-01 09:49:49.965: INFO/DEBUG(30):          #12  pc 0020ce24  /system/lib/libwebcore.so
09-01 09:49:49.976: INFO/DEBUG(30):          #13  pc 001a0966  /system/lib/libwebcore.so
09-01 09:49:49.976: INFO/DEBUG(30):          #14  pc 001a6ef4  /system/lib/libwebcore.so
09-01 09:49:49.985: INFO/DEBUG(30):          #15  pc 001a4cd4  /system/lib/libwebcore.so
09-01 09:49:49.985: INFO/DEBUG(30):          #16  pc 001a4e1a  /system/lib/libwebcore.so
09-01 09:49:50.015: INFO/DEBUG(30):          #17  pc 001a80a2  /system/lib/libwebcore.so
09-01 09:49:50.015: INFO/DEBUG(30):          #18  pc 001a8236  /syst开发者_开发问答em/lib/libwebcore.so
09-01 09:49:50.015: INFO/DEBUG(30):          #19  pc 00065108  /system/lib/libwebcore.so
09-01 09:49:50.025: INFO/DEBUG(30):          #20  pc 00065cae  /system/lib/libwebcore.so
09-01 09:49:50.045: INFO/DEBUG(30):          #21  pc 001254e2  /system/lib/libwebcore.so
09-01 09:49:50.045: INFO/DEBUG(30):          #22  pc 001255da  /system/lib/libwebcore.so
09-01 09:49:50.066: INFO/DEBUG(30):          #23  pc 00125600  /system/lib/libwebcore.so
09-01 09:49:50.075: INFO/DEBUG(30):          #24  pc 00017d74  /system/lib/libdvm.so
09-01 09:49:50.085: INFO/DEBUG(30): code around pc:
09-01 09:49:50.085: INFO/DEBUG(30): 80045a28 447a4479 ed0cf7d1 20004c09 ee34f7d1 
09-01 09:49:50.095: INFO/DEBUG(30): 80045a38 447c4808 6bdb5823 d0002b00 49064798 
09-01 09:49:50.095: INFO/DEBUG(30): 80045a48 700a2226 eea0f7d1 000436b7 00045275 
09-01 09:49:50.095: INFO/DEBUG(30): 80045a58 0005eb82 fffffec4 deadd00d b510b40e 
09-01 09:49:50.105: INFO/DEBUG(30): 80045a68 4c0a4b09 447bb083 aa05591b 6b5bca02 
09-01 09:49:50.105: INFO/DEBUG(30): code around lr:
09-01 09:49:50.105: INFO/DEBUG(30): afd19354 b0834a0d 589c447b 26009001 686768a5 
09-01 09:49:50.105: INFO/DEBUG(30): afd19364 220ce008 2b005eab 1c28d003 47889901 
09-01 09:49:50.116: INFO/DEBUG(30): afd19374 35544306 d5f43f01 2c006824 b003d1ee 
09-01 09:49:50.116: INFO/DEBUG(30): afd19384 bdf01c30 000281a8 ffffff88 1c0fb5f0 
09-01 09:49:50.116: INFO/DEBUG(30): afd19394 43551c3d a904b087 1c16ac01 604d9004 
09-01 09:49:50.116: INFO/DEBUG(30): stack:
09-01 09:49:50.116: INFO/DEBUG(30):     440912b0  00000015  
09-01 09:49:50.116: INFO/DEBUG(30):     440912b4  afd18407  /system/lib/libc.so
09-01 09:49:50.125: INFO/DEBUG(30):     440912b8  afd4270c  /system/lib/libc.so
09-01 09:49:50.125: INFO/DEBUG(30):     440912bc  afd426b8  /system/lib/libc.so
09-01 09:49:50.125: INFO/DEBUG(30):     440912c0  00000000  
09-01 09:49:50.125: INFO/DEBUG(30):     440912c4  afd19375  /system/lib/libc.so
09-01 09:49:50.125: INFO/DEBUG(30):     440912c8  0029e2c0  [heap]
09-01 09:49:50.125: INFO/DEBUG(30):     440912cc  afd183d9  /system/lib/libc.so
09-01 09:49:50.135: INFO/DEBUG(30):     440912d0  0029ecf0  [heap]
09-01 09:49:50.135: INFO/DEBUG(30):     440912d4  0005eb82  [heap]
09-01 09:49:50.135: INFO/DEBUG(30):     440912d8  40542240  /dev/ashmem/dalvik-heap (deleted)
09-01 09:49:50.135: INFO/DEBUG(30):     440912dc  80085acc  /system/lib/libdvm.so
09-01 09:49:50.135: INFO/DEBUG(30):     440912e0  0029ecf0  [heap]
09-01 09:49:50.135: INFO/DEBUG(30):     440912e4  afd18437  /system/lib/libc.so
09-01 09:49:50.145: INFO/DEBUG(30):     440912e8  df002777  
09-01 09:49:50.145: INFO/DEBUG(30):     440912ec  e3a070ad  
09-01 09:49:50.145: INFO/DEBUG(30): #00 440912f0  00000001  
09-01 09:49:50.145: INFO/DEBUG(30):     440912f4  8003774d  /system/lib/libdvm.so
09-01 09:49:50.145: INFO/DEBUG(30): #01 440912f8  00000001  
09-01 09:49:50.145: INFO/DEBUG(30):     440912fc  80039a15  /system/lib/libdvm.so
09-01 09:49:52.566: INFO/BootReceiver(60): Copying /data/tombstones/tombstone_00 to DropBox (SYSTEM_TOMBSTONE)
09-01 09:49:52.596: INFO/DEBUG(30): debuggerd committing suicide to free the zombie!
09-01 09:49:52.646: INFO/DEBUG(379): debuggerd: Feb  3 2011 14:45:34
09-01 09:49:52.685: DEBUG/dalvikvm(60): GC_FOR_MALLOC freed 1015K, 51% free 4407K/8967K, external 3520K/4385K, paused 104ms
09-01 09:49:52.776: DEBUG/dalvikvm(130): GC_EXPLICIT freed 19K, 52% free 2888K/5895K, external 5582K/6620K, paused 1008ms
09-01 09:49:52.786: DEBUG/Zygote(32): Process 366 terminated by signal (11)
09-01 09:49:52.796: INFO/ActivityManager(60): Process com.hello (pid 366) has died.
09-01 09:49:52.806: ERROR/InputDispatcher(60): channel '406b7e10 com.hello/com.hello.HelloWebView (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
09-01 09:49:52.806: ERROR/InputDispatcher(60): channel '406b7e10 com.hello/com.hello.HelloWebView (server)' ~ Channel is unrecoverably broken and will be disposed!
09-01 09:49:52.826: INFO/WindowManager(60): WIN DEATH: Window{406b7e10 com.hello/com.hello.HelloWebView paused=false}

UPDATE 1

I have successfully gotten the Toast on devices with 2.3.4 and higher. But still not on my emulator.

UPDATE 2

I have the same problem on an emulator with 2.3.4 on it. But no problem running it on an emulator with 2.2 on it.


http://code.google.com/p/android/issues/detail?id=12987


try this: http://quitenoteworthy.blogspot.com/2010/12/handling-android-23-webviews-broken.html this work for me....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜