2

我正在开发一个混合 webview/native 应用程序,并且遇到了应用程序崩溃的问题。

这是快速纲要:

  • 应用程序位于单个 FrameLayout
  • 顶部有一个 LinearLayout,我在左侧和右侧放置一个按钮,在中间和下方放置我的应用程序徽标,我有我的 WebView
  • 我正在使用webview.addJavascriptInterface(myObj, "myJsName")将 JS 接口附加到 Web 视图
  • 在 webView 中,我加载了一个简单的 HTML 页面,该页面在我的 JS 界面中调用公共方法。这些方法只是在虚假的“标题栏”(LineadLayout)中显示或隐藏各种视图
  • 我的 JS 界面中的方法调用得很好 - 但是如果我尝试在这些调用中更新 UI,应用程序会崩溃并出现Fatal signal 11 (SIGSEGV)

这是我活动中的相关代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    webview = (WebView) findViewById(R.id.webview);
    logo = (ImageView) findViewById(R.id.logo);

    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setLightTouchEnabled(true);
    webview.getSettings().setEnableSmoothTransition(true);

    javascriptInterface = new JavascriptInterface() {
        public void showLogo() {
            runOnUiThread(new Runnable() {
                public void run() {
                    MyActivity.this.logo.setVisibility(View.VISIBLE);
                }
            });
        }

        public void hideLogo() {
            MyActivity.this.runOnUiThread( new Runnable() {
                public void run() {
                    MyActivity.this.logo.setVisibility(View.GONE);
                }
            });
        }
    };
    webview.addJavascriptInterface(javascriptInterface, JavascriptInterface.NAME);
    webview.loadUrl("file:///android_asset/test.html");
}
  • 从我的 JS 代码调用hideLogo()时,应用程序崩溃并显示Fatal signal 11 (SIGSEGV).
  • 如果我删除MyActivity.this.runOnUiThread并尝试修改 UI 没有任何反应(也没有崩溃)
  • 调试时,我保证该logo变量不为空,并且确实代表正确的 ImageView
  • hideLogo()此外,在应用程序崩溃之前调用徽标确实会消失

所以,现在我真的很困惑。我试过使用 aHandlerAsycTask& 都给我同样类型的崩溃。

我对 Android 非常陌生,所以我希望我在这里做一些愚蠢的事情。任何和所有的帮助表示赞赏!

更新这里是 log cat 的详细输出

10-01 12:29:55.591: INFO/Choreographer(10074): Skipped 74 frames!  The application may be doing too much work on its main thread.
10-01 12:29:55.622: null/libc(10074): Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1), thread 10074 (com.arin.test)
10-01 12:29:55.724: INFO/DEBUG(121): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
10-01 12:29:55.724: INFO/DEBUG(121): Build fingerprint: 'google/takju/maguro:4.1.1/JRO03C/398337:user/release-keys'
10-01 12:29:55.724: INFO/DEBUG(121): pid: 10074, tid: 10074, name: com.arin.test  >>> com.arin.test <<<
10-01 12:29:55.724: INFO/DEBUG(121): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000008
10-01 12:29:55.849: INFO/DEBUG(121): r0 00000008  r1 5bc3ebc0  r2 00000010  r3 00000004
10-01 12:29:55.849: INFO/DEBUG(121): r4 5a52d008  r5 5a52de38  r6 00000001  r7 00000004
10-01 12:29:55.849: INFO/DEBUG(121): r8 00000008  r9 00000001  sl 00000004  fp 00000001
10-01 12:29:55.849: INFO/DEBUG(121): ip 00000001  sp bedb2310  lr 5bea188c  pc 5beca774  cpsr 80000010
10-01 12:29:55.849: INFO/DEBUG(121): d0  00000000bd0d3d00  d1  3f8469ee00000000
10-01 12:29:55.849: INFO/DEBUG(121): d2  0000000000000080  d3  000000000000003f
10-01 12:29:55.849: INFO/DEBUG(121): d4  0000000000000000  d5  000000003f800000
10-01 12:29:55.849: INFO/DEBUG(121): d6  3f8469eebd0d3dcb  d7  3f8000003f800000
10-01 12:29:55.849: INFO/DEBUG(121): d8  bf800000bf800000  d9  0000000000000000
10-01 12:29:55.849: INFO/DEBUG(121): d10 0000000000000000  d11 0000000000000000
10-01 12:29:55.849: INFO/DEBUG(121): d12 0000000000000000  d13 0000000000000000
10-01 12:29:55.849: INFO/DEBUG(121): d14 0000000000000000  d15 0000000000000000
10-01 12:29:55.849: INFO/DEBUG(121): d16 0000000000000008  d17 000000c1417d8568
10-01 12:29:55.849: INFO/DEBUG(121): d18 000000000358b208  d19 0000000000000000
10-01 12:29:55.849: INFO/DEBUG(121): d20 0000000000000000  d21 0000000000000000
10-01 12:29:55.849: INFO/DEBUG(121): d22 0000000000000000  d23 0000000000000000
10-01 12:29:55.849: INFO/DEBUG(121): d24 3fee45a1e2e00000  d25 4000000006800000
10-01 12:29:55.849: INFO/DEBUG(121): d26 0000000000000000  d27 0000000000000000
10-01 12:29:55.849: INFO/DEBUG(121): d28 408d900000000000  d29 0000000000000000
10-01 12:29:55.849: INFO/DEBUG(121): d30 bffffffff8a00000  d31 0000000000000000
10-01 12:29:55.849: INFO/DEBUG(121): scr 20000010
10-01 12:29:55.849: INFO/DEBUG(121): backtrace:
10-01 12:29:55.849: INFO/DEBUG(121): #00  pc 00034774  /system/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
10-01 12:29:55.849: INFO/DEBUG(121): #01  pc 0000b888  /system/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
10-01 12:29:55.849: INFO/DEBUG(121): stack:
10-01 12:29:55.849: INFO/DEBUG(121): bedb22d0  03d00000
10-01 12:29:55.849: INFO/DEBUG(121): bedb22d4  00000000
10-01 12:29:55.849: INFO/DEBUG(121): bedb22d8  5a4defc8
10-01 12:29:55.849: INFO/DEBUG(121): bedb22dc  5a5316c0
10-01 12:29:55.849: INFO/DEBUG(121): bedb22e0  5a52dee4
10-01 12:29:55.849: INFO/DEBUG(121): bedb22e4  00000008
10-01 12:29:55.849: INFO/DEBUG(121): bedb22e8  00000002
10-01 12:29:55.849: INFO/DEBUG(121): bedb22ec  00000004
10-01 12:29:55.849: INFO/DEBUG(121): bedb22f0  5a52dee4
10-01 12:29:55.849: INFO/DEBUG(121): bedb22f4  5a52d008
10-01 12:29:55.849: INFO/DEBUG(121): bedb22f8  5a52de34
10-01 12:29:55.849: INFO/DEBUG(121): bedb22fc  00000000
10-01 12:29:55.849: INFO/DEBUG(121): bedb2300  00000004
10-01 12:29:55.849: INFO/DEBUG(121): bedb2304  00000023
10-01 12:29:55.849: INFO/DEBUG(121): bedb2308  df0027ad
10-01 12:29:55.849: INFO/DEBUG(121): bedb230c  00000000
10-01 12:29:55.849: INFO/DEBUG(121): #00  bedb2310  5a52d008
10-01 12:29:55.849: INFO/DEBUG(121): ........  ........
10-01 12:29:55.849: INFO/DEBUG(121): #01  bedb2310  5a52d008
10-01 12:29:55.849: INFO/DEBUG(121): bedb2314  5bea188c  /system/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
10-01 12:29:55.849: INFO/DEBUG(121): bedb2318  5a52d008
10-01 12:29:55.849: INFO/DEBUG(121): bedb231c  00000004
10-01 12:29:55.849: INFO/DEBUG(121): bedb2320  00000004
10-01 12:29:55.849: INFO/DEBUG(121): bedb2324  00000000
10-01 12:29:55.849: INFO/DEBUG(121): bedb2328  00000005
10-01 12:29:55.849: INFO/DEBUG(121): bedb232c  5bea398c  /system/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
10-01 12:29:55.849: INFO/DEBUG(121): bedb2330  5a52d008
10-01 12:29:55.849: INFO/DEBUG(121): bedb2334  00000004
10-01 12:29:55.849: INFO/DEBUG(121): bedb2338  00000000
10-01 12:29:55.849: INFO/DEBUG(121): bedb233c  5bef106c  /system/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
10-01 12:29:55.849: INFO/DEBUG(121): bedb2340  00000005
10-01 12:29:55.849: INFO/DEBUG(121): bedb2344  5bea4a70  /system/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so (glDrawArrays+804)
10-01 12:29:55.849: INFO/DEBUG(121): bedb2348  00000004
10-01 12:29:55.849: INFO/DEBUG(121): bedb234c  00000000
10-01 12:29:55.849: INFO/DEBUG(121): memory near r1:
10-01 12:29:55.849: INFO/DEBUG(121): 5bc3eba0 ffffffff ffffffff ffffffff ffffffff  ................
10-01 12:29:55.849: INFO/DEBUG(121): 5bc3ebb0 ffffffff ffffffff ffffffff ffffffff  ................
10-01 12:29:55.849: INFO/DEBUG(121): 5bc3ebc0 ffffffff ffffffff ffffffff ffffffff  ................
10-01 12:29:55.849: INFO/DEBUG(121): 5bc3ebd0 ffffffff ffffffff ffffffff ffffffff  ................
10-01 12:29:55.849: INFO/DEBUG(121): 5bc3ebe0 ffffffff ffffffff ffffffff ffffffff  ................
10-01 12:29:55.849: INFO/DEBUG(121): memory near r4:
10-01 12:29:55.849: INFO/DEBUG(121): 5a52cfe8 5a525fe0 00000001 00000000 00000007  ._RZ............
10-01 12:29:55.849: INFO/DEBUG(121): 5a52cff8 00000007 00000007 00000000 00000f9b  ................
10-01 12:29:55.849: INFO/DEBUG(121): 5a52d008 00000018 0000e7f4 00001c80 00001100  ................
10-01 12:29:55.849: INFO/DEBUG(121): 5a52d018 00001100 0000000a 00000428 00000048  ........(...H...
10-01 12:29:55.849: INFO/DEBUG(121): 5a52d028 0000003c 00000048 0000003c 00000004  <...H...<.......
10-01 12:29:55.849: INFO/DEBUG(121): memory near r5:
10-01 12:29:55.849: INFO/DEBUG(121): 5a52de18 00000000 00000000 00000000 00000000  ................
10-01 12:29:55.849: INFO/DEBUG(121): 5a52de28 00000000 00000000 5a52dc0c 5a52dcb4  ..........RZ..RZ
10-01 12:29:55.849: INFO/DEBUG(121): 5a52de38 5a52dce4 00000000 00000000 00000000  ..RZ............
10-01 12:29:55.849: INFO/DEBUG(121): 5a52de48 00000000 00000000 00000000 00000000  ................
10-01 12:29:55.849: INFO/DEBUG(121): 5a52de58 00000002 00000003 00000003 00000004  ................
10-01 12:29:55.849: INFO/DEBUG(121): memory near sp:
10-01 12:29:55.849: INFO/DEBUG(121): bedb22f0 5a52dee4 5a52d008 5a52de34 00000000  ..RZ..RZ4.RZ....
10-01 12:29:55.849: INFO/DEBUG(121): bedb2300 00000004 00000023 df0027ad 00000000  ....#....'......
10-01 12:29:55.849: INFO/DEBUG(121): bedb2310 5a52d008 5bea188c 5a52d008 00000004  ..RZ...[..RZ....
10-01 12:29:55.849: INFO/DEBUG(121): bedb2320 00000004 00000000 00000005 5bea398c  .............9.[
10-01 12:29:55.849: INFO/DEBUG(121): bedb2330 5a52d008 00000004 00000000 5bef106c  ..RZ........l..[
10-01 12:29:55.849: INFO/DEBUG(121): code around pc:
10-01 12:29:55.849: INFO/DEBUG(121): 5beca754 e781410c e28cc001 e15c0003 3afffffa  .A........\....:
10-01 12:29:55.849: INFO/DEBUG(121): 5beca764 e8bd8010 e92d4010 e3a0c000 ea000004  .....@-.........
10-01 12:29:55.849: INFO/DEBUG(121): 5beca774 e5904000 e5014008 e5904004 e0800002  .@...@...@......
10-01 12:29:55.849: INFO/DEBUG(121): 5beca784 e5014004 e15c0003 e2811008 e28cc001  .@....\.........
10-01 12:29:55.849: INFO/DEBUG(121): 5beca794 3afffff6 e8bd8010 e92d4010 e3a0c000  ...:.....@-.....
10-01 12:29:55.856: INFO/DEBUG(121): code around lr:
10-01 12:29:55.856: INFO/DEBUG(121): 5bea186c e3530000 1a000005 e1a03007 e59c0018  ..S......0......
10-01 12:29:55.856: INFO/DEBUG(121): 5bea187c e59c101c e59c2014 e1a0e00f e59cf024  ..... ......$...
10-01 12:29:55.856: INFO/DEBUG(121): 5bea188c e2866001 e2855004 e5942e50 e3a03001  .`...P..P....0..
10-01 12:29:55.856: INFO/DEBUG(121): 5bea189c e1560002 3affffeb e2840eed e1a01008  ..V....:........
10-01 12:29:55.856: INFO/DEBUG(121): 5bea18ac e280000c e1a02003 e8bd41f0 eaffe178  ..... ...A..x...
10-01 12:29:56.060: INFO/WindowState(321): WIN DEATH: Window{425993f8 com.arin.test/com.arin.test paused=false}
10-01 12:29:56.060: INFO/ActivityManager(321): Process com.arin.test (pid 10074) has died.
10-01 12:29:56.060: WARN/ActivityManager(321): Force removing ActivityRecord{4226e5d0 com.arin.test/com.arin.test}: app died, no saved state
10-01 12:29:56.099: DEBUG/Zygote(124): Process 10074 terminated by signal (11)
10-01 12:29:56.161: WARN/InputMethodManagerService(321): Got RemoteException sending setActive(false) notification to pid 10074 uid 10008
4

3 回答 3

2

我遇到了同样的问题。使用处理程序修复它:

class MyJavaScriptInterface   
    {  
        // gets called from JavasScript
        @SuppressWarnings("unused")  
        public void notifyStarted()  
        {  
            _handler.post(_onNavigationStarted);
        }  
    } 

    final Handler _handler = new Handler();

    final Runnable _onNavigationStarted = new Runnable() {
        public void run() {
            _me.navigationStarted();
        }
    };

    protected void navigationStarted() {
        _btnNavigate.setEnabled(true);
        _btnNavigate.setText(_me.getResources().getString(R.string.Navigate));
    }
于 2012-11-01T14:10:13.343 回答
0

这是我使用的代码。这是一个简单的示例,它将显示Toast. 试试这个,看看它是否有效。

在你的onCreate()

webView = (WebView) findViewById( R.id.webView );
webView.getSettings().setJavaScriptEnabled( true );
webView.addJavascriptInterface( new JavascriptInterface(), "Android" );
webview.loadUrl("file:///android_asset/test.html");

您的JavascriptInterface(在Activity使用它的内部定义):

private class JavascriptInterface {

    public void makeToast( String toastText ) {

        Toast.makeText( getApplicationContext(), toastText, Toast.LENGTH_LONG ).show();
    }
}

在您的 javascript 中:

function makeToast() {

    Android.makeToast("Test");
}
于 2012-10-01T20:00:20.530 回答
0

好的,我想通了。我不知道这是如何发生或为什么发生的——但一种不同的方法解决了这个问题:

  • 我使用的是单个视图 xml 文件 ( main.xml)。视图的顶部 45dp 专用于模拟标题栏的 RelativeLayout,其余的是 WebView
  • 我将所有虚假标题栏元素移出main.xml并创建了一个自定义标题栏 ( title.xml)。这解决了我的问题

我不知道为什么一开始它就坏了,但现在它起作用了,我可以继续我的生活了。

另一件值得注意的事情是,在调试时,我添加了一堆本机控件,它们进行的调用与 HTML 按钮通过 JS 界面调用的调用相同,并且运行良好 100%。

每次我尝试通过 JS 接口修改视图属性时,最初的方法都会导致疯狂的内存访问违规。

于 2012-10-01T22:15:29.580 回答