user1362015
user1362015

Reputation: 53

webView.loadUrl is not working

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.

Upvotes: 5

Views: 12911

Answers (2)

Sreedev
Sreedev

Reputation: 6653

// 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.

Upvotes: 9

Shankar Agarwal
Shankar Agarwal

Reputation: 34775

instead of loadUrl, try using the loadDataWithBaseURL method:

webview.loadDataWithBaseURL("android.resource://<package_name>/assets/www/file_name", html, mimeType, encoding, "");

Upvotes: 1

Related Questions