5

This is my code:

package sai.datla.game;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class GametestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.clearSslPreferences();
        webView.loadUrl("file:///android_asset/www/index.html");
   }
}

but it is saying that it cannot find the link in the emulator. I have checked many times if my index.html file is in the www folder, which is in my assets, but it won't work. Please help.

By the way I am only twelve years old so please make the answers easy to understand for a child.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
user1362015
  • 53
  • 1
  • 1
  • 3
  • 1
    there is a similar question http://stackoverflow.com/questions/5320288/displaying-android-asset-files-in-a-webview have you tried something like that? – marwinXXII Apr 27 '12 at 21:59

2 Answers2

9
// Find view in layout
WebView wv = (WebView) findViewById(R.id.webView_tobe_loaded);    

// Get settings
WebSettings wbset = wv.getSettings();

// Enable JavaScript
wbset.setJavaScriptEnabled(true);

// Set new client (to handle website in your app)
wv.setWebViewClient(new MyWebViewClient());

// Example of the URL
String url = "http://www.google.com";

// Load website
wv.loadUrl(url);

This code will help you to solve the problem. It worked for me.

Boken
  • 4,825
  • 10
  • 32
  • 42
Sreedev
  • 6,563
  • 5
  • 43
  • 66
1

instead of loadUrl, try using the loadDataWithBaseURL method:

webview.loadDataWithBaseURL("android.resource://<package_name>/assets/www/file_name", html, mimeType, encoding, "");
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64