3

我开发了一个移动 HTML5 游戏,当我从 Web 加载 URL 时它运行良好。但是,当我在 Android 4.0 上显示从 assets 文件夹中调用 JavaScript 的 index.html 时,它不起作用。我在下面包含了 logcat 和代码。

public class BlockyBlaine extends Activity {
    WebView webview;
    AdView adView;

    private class BlaineViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }


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

        adView = (AdView) findViewById(R.id.adView);

        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setPluginsEnabled(true);
        webview.getSettings().setSupportZoom(false);
        webview.setVerticalScrollBarEnabled(false);
        webview.setHorizontalScrollBarEnabled(false);
        webview.getSettings().setUseWideViewPort(false);
        webview.getSettings().setDomStorageEnabled(true);
        webview.loadUrl("file:///android_asset/www/index.html");
        webview.setWebViewClient(new BlaineViewClient());
        webview.setFocusableInTouchMode(false);
        adView.loadAd(new AdRequest());
    }
}

06-09 19:06:06.275: I/Ads(1342): To get test ads on this device, call adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
06-09 19:06:06.675: I/Ads(1342): adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>AFMA_getSdkConstants();AFMA_buildAdURL({"preqs":0,"session_id":"8885582241455077410","u_sd":1.5,"seq_num":"1","slotname":"a14fd0f1400116b","u_w":320,"msid":"com.blockyblaine.bobhoil","simulator":1,"cap":"m,a","js":"afma-sdk-a-v6.0.1","isu":"B3EEABB8EE11C2BE770B684D95219ECB","cipa":0,"format":"320x50_mb","net":"ed","app_name":"1.android.com.blockyblaine.bobhoil","hl":"en","u_h":533,"carrier":"310260","ptime":0,"u_audio":4});</script></head><body></body></html>
06-09 19:06:06.715: D/gralloc_goldfish(1342): Emulator without GPU emulation detected.
06-09 19:06:06.765: D/dalvikvm(1342): GC_CONCURRENT freed 220K, 4% free 10110K/10439K, paused 5ms+7ms
06-09 19:06:07.375: D/chromium(1342): Unknown chromium error: -6
06-09 19:06:07.515: E/libEGL(1342): call to OpenGL ES API with no current context (logged once per thread)
06-09 19:06:07.515: D/ShaderProgram(1342): couldn't load the vertex shader!
06-09 19:06:07.525: E/libEGL(1342): call to OpenGL ES API with no current context (logged once per thread)
06-09 19:06:07.525: D/ShaderProgram(1342): couldn't load the vertex shader!
06-09 19:06:07.525: E/libEGL(1342): call to OpenGL ES API with no current context (logged once per thread)
06-09 19:06:07.535: D/ShaderProgram(1342): couldn't load the vertex shader!
06-09 19:06:07.535: E/libEGL(1342): call to OpenGL ES API with no current context (logged once per thread)
06-09 19:06:07.535: D/ShaderProgram(1342): couldn't load the vertex shader!
06-09 19:06:07.545: E/libEGL(1342): call to OpenGL ES API with no current context (logged once per thread)
06-09 19:06:07.555: D/ShaderProgram(1342): couldn't load the vertex shader!
4

3 回答 3

2

这只是在黑暗中拍摄,但看起来你的模拟器需要你打电话,

AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);

打电话之前,

adView.loadAd(adRequest);

编辑:

阅读这个这个。看起来 Android 团队决定“file:///scheme”是不安全的(从 4.0 开始)。

也许您可以将资产上传到保管箱(或在线某处),然后在运行时下载文件。

于 2012-06-10T16:04:51.097 回答
0

也许您可以将 javascript 放入标签中,并将所需的任何资产编码为 base64 编码变量。这样,它将是一个文件,您可以以任何您认为合适的方式将其加载到 webview 中。一旦你让它工作,你会想要自动化这个过程。

不过,谷歌决定毁掉 android 4.0 的 webview 似乎很可惜。幸运的是,windows 8 很快就出来了。

于 2012-06-20T16:55:44.963 回答
0

这可能会迟到,但是默认的 Android webview 无法加载以下划线“_”开头的文件。正是这一点导致Unknown chromium error: -6

我在使用 Phonegap 时遇到了同样的问题。将你所有的 javascript 合并到一个文件中可以避免这个问题,并可能使你的应用程序加载更有效。

于 2013-10-22T09:39:53.293 回答