Android WebView错误无法初始化浏览器着色器磁盘缓存

时间:2016-01-06 05:41:55

标签: android-webview

我的代码在webView中打开index.html文件但无法打开css和js文件,我得到的错误是:

  

E / SysUtils:ApplicationStat中的ApplicationContext为null   E / chromium:[错误:browser_gpu_channel_host_factory.cc(258)]无法初始化浏览器着色器磁盘缓存。   E / libEGL:validate_display:255错误3008(EGL_BAD_DISPLAY)

index.html的<head>中引用的所有文件都作为index.html的兄弟姐妹位于assets文件夹中

package au.com.totalcareauto.webappandroid1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    WebView wv = (WebView) findViewById(R.id.wv);
    wv.setWebViewClient(new WebViewClient());
    WebSettings ws = wv.getSettings();
    ws.setJavaScriptEnabled(true);
    ws.setAllowFileAccess(true);

    String summary = null;
    String path = "file:///assets/";
    try {
        summary = getStringFromFile("index.html");
    } catch (Exception e) {
        e.printStackTrace();
    }
    wv.loadDataWithBaseURL(path ,summary,"text/html","utf-8",null);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
    }
    reader.close();
    return sb.toString();
}

public String getStringFromFile(String filePath) throws Exception {
    File fl = new File(filePath);
    String ret = convertStreamToString(this.getAssets().open(filePath));
    return ret;
}
}

   <head>
      <meta charset="UTF-8">
      <title>RRR</title>
      <link type="text/css" rel="stylesheet" href="jquery.mobile-1.4.5.css"/>
      <link type="text/css" rel="stylesheet" href="index.css"/>
      <script type="text/javascript" src="jquery-1.11.3.js"></script>
      <script type="text/javascript" src="jquery.mobile-1.4.5.js"></script>
      <meta name="viewport" content="width=device-width"/>
   </head>

1 个答案:

答案 0 :(得分:0)

日志不反映加载失败的原因。

问题是你错路:

String path = "file:///assets/";

应该是:

String path = "file:///android_asset/";