我开发了一款移动HTML5游戏,当我从网上加载网址时,该游戏运行正常。但是当我显示从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!
答案 0 :(得分:2)
这只是一个黑暗的镜头,但看起来你的模拟器需要你打电话,
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
在致电之前,
adView.loadAd(adRequest);
阅读this和this。看起来Android团队决定&#34; file:/// scheme&#34;是不安全的(从4.0开始)。
也许您只需将资产上传到dropbox(或在线某处),然后在运行时下载该文件。
答案 1 :(得分:0)
也许您可以将javascript放入标记中,并将所需的任何资产编码为base64编码变量。这样它就是一个单独的文件,您可以以任何您认为合适的方式加载到webview中。一旦你开始工作,你将希望自动化这个过程。
谷歌决定破坏webview for android 4.0似乎很遗憾。幸运的是,Windows 8即将推出。
答案 2 :(得分:0)
这可能会迟到,但是默认的Android webview无法加载前面带有下划线“_”的文件。这是导致Unknown chromium error: -6
使用Phonegap时遇到同样的问题。将所有javascript组合到一个文件中可以解决这个问题,并可能使您的应用程序更有效地加载。