3

I developed a mobile HTML5 game which works fine when I load the URL from the web. But when I display the index.html which calls the JavaScript from the assets folder on Android 4.0 it does not work. I have included logcat and code below.

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!
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Greg Froning
  • 213
  • 1
  • 4
  • 16
  • Have you ever managed to solve this? I think I'm having the same problem, when loading content with `file:///` schema. – MonkeyCoder Mar 14 '13 at 09:23

3 Answers3

2

This is just a shot in the dark here, but it looks like your emulator requires you to call,

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

before calling,

adView.loadAd(adRequest);

Edit:

Read this and this. It looks like the Android team came to the decision that "file:///scheme" is insecure (beginning with 4.0).

Perhaps you could just upload the asset to dropbox (or somewhere online) instead and download the file at runtime.

Community
  • 1
  • 1
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Thanks for the help but that didn't seem to fix the problem. I think it has to do with calling javascript via the assets folder locally. Mainly what makes me think this is the error: 06-09 19:06:07.375: D/chromium(1342): Unknown chromium error: -6 – Greg Froning Jun 10 '12 at 16:15
  • Have you seen [this post](http://stackoverflow.com/a/4820905/844882)? Have you tested it on a real device? What SDK version are you testing with? – Alex Lockwood Jun 10 '12 at 16:24
  • Yes I have tested on two different devices and also have had others test it on devices. It has to do specifically with 4.0 it seems. The index.html is loading I think but not calling the javascript .js file once loaded? – Greg Froning Jun 10 '12 at 16:34
  • Read [**this**](http://stackoverflow.com/questions/8390985/android-4-0-1-breaks-webview-html-5-local-storage) and [**this**](http://stackoverflow.com/questions/9105913/android-4-0-webview-loadurl-oddity). It looks like the Android team came to the decision that "file:///scheme" is insecure (beginning with 4.0). Perhaps you could just upload the asset to dropbox (or somewhere online) instead and download the file at runtime. – Alex Lockwood Jun 10 '12 at 16:38
  • Well I would like the user to not have to be online. If webserver goes down or something like that the game would be unavailable. Im not sure how to go about using loaddatawithbaseurl to fix the problem though. – Greg Froning Jun 10 '12 at 16:44
  • I'm still trying to find something online that works but haven't had any luck so far. If you never end up figuring something out (although there *must* be a workaround... there has to be!), what you could do is require the user to download the html the first time he opens the app and then save the html to local storage. Then from that point forward the user would be opening the html5 from there? This wouldn't be too stringent probably... as presumably the user would be opening the app immediately after installing it (thus they will probably have a network connection). Just an idea... – Alex Lockwood Jun 10 '12 at 17:00
  • Actually, on second thought even that might not work because you'd still be attempting to access the file w/ a "file:///" uri... damn!! – Alex Lockwood Jun 10 '12 at 17:01
  • Yeah I am having a hard time with this. The game performs better when loaded locally for some reason so this would be ideal. I am frying to figure out something to work around the problem. Just haven't quite figured it out yet! – Greg Froning Jun 12 '12 at 02:37
0

Perhaps you can put the javascript into tags, and encode any assets required as base64 encoded variables. That way it'll be one single file, which you can load into the webview any way you see fit. You will want to automate this process once you get it working.

It does seem a shame that google decided to ruin webview for android 4.0 though. Luckily windows 8 comes out soon.

fabspro
  • 1,639
  • 19
  • 29
0

This might be late, however the default Android webview has trouble loading files that are preceded with a underscore, "_". It is this that causes the Unknown chromium error: -6

I had this same problem while using Phonegap. Combining all you javascript into one file will circumvent this problem and probably make your app load more efficiently.