1

My android WebView is showing a blank page when I try to load "file:///android_asset/index.html" and it perfectly loads other url like "http://twitter.com".

My activity

public class MainActivity  extends ActionBarActivity  {

    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = (WebView) findViewById(R.id.webview);

        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        myWebView.loadUrl("file:///android_asset/index.html");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.loadUrl("file:///android_asset/index.html");
            return true;
        }
        return false;
    }
}

My index.html file :

<!DOCTYPE html>
<html>
<head>
    <title>World Tour</title>
    <!-- Sets initial viewport load and disables zooming  -->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">

    <!-- Makes your prototype chrome-less once bookmarked to your phone's home screen -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="mobile-web-app-capable" content="yes">

    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" type="text/css" href="css/ratchet.css">
</head>
<body>
    <header class="bar bar-footer">
        <a href="views/stats.html" data-transition="slide-out"><span class="icon icon-info pull-left" ></span></a>
        <a href="views/add.html" data-transition="slide-in"><span class="icon icon-plus pull-right" ></span></a>
        <h1 class="title">List of previous drive</h1>
    </header>

    <div class="content">
        <div class="card">
            <ul id="drives-list" class="table-view">
                <li class="table-view-divider table-view-cell" ><p class="pull-left">Comments</p><p class="pull-right">Distance</p></li>
            </ul>
        </div>
    </div>

    <script type="text/javascript" src="js/main.js" ></script>
    <script type="text/javascript" src="js/ratchet.js" ></script>
</body>
</html>

Thanks for helping !

Pangoraw
  • 70
  • 1
  • 8
  • is there any folder in which you put your index.html? if so, you have to add that folder name in your path as well. – Yauraw Gadav Apr 04 '15 at 09:23
  • it is in the assets folder which can be accessed with file:///android_asset/ – Pangoraw Apr 04 '15 at 10:11
  • you seemed to have used it right, not sure..maybe try to clean the project and run again.. – Yauraw Gadav Apr 04 '15 at 10:15
  • It still does not work. I tried making a new project but it is still not working. I am trying to run it under Lollipop. – Pangoraw Apr 04 '15 at 10:58
  • 1
    Ok, I tried a with new simple html file. It worked. The problem may be linked with ratchet, which has not been updated for so long. – Pangoraw Apr 04 '15 at 11:05

2 Answers2

0

get string content from your assests file using below code

    public static String getStringFromAssets(String name, Context p_context)
    {
        try
        {
            InputStream is = p_context.getAssets().open(name);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String bufferString = new String(buffer);
            return bufferString;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;

    }

and load content using below code

String m_data = getStringFromAssets("index.html", this);
webView.loadDataWithBaseURL("file:///android_asset/", m_data, "text/html", "utf-8", null);

Hope it works for you !!

  • It doesn't work as well. I am sure the page is loaded because I can use console.log with javascript and it works. – Pangoraw Apr 04 '15 at 10:10
0

Checkout if there are any errors in javascript code associated with the html page. If so android studio doesn't point out errors in js file.

I fixed the errors in the js file and it worked.

Saikrishna Rajaraman
  • 3,205
  • 2
  • 16
  • 29