Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 575
user1362015 Avatar asked Apr 27 '12 21:04

user1362015


People also ask

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

How do I enable JavaScript on WebView?

This example demonstrate about How to enable webview java script in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I send a post request in WebView?

webView. setWebViewClient(new WebViewClient(){ public void onPageStarted(WebView view, String url, Bitmap favicon) { super. onPageStarted(view, url, favicon); } public boolean shouldOverrideUrlLoading(WebView view, String url) { webView. postUrl(Base_Url, postData.


Video Answer


2 Answers

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

like image 115
Sreedev Avatar answered Nov 11 '22 21:11

Sreedev


instead of loadUrl, try using the loadDataWithBaseURL method:

webview.loadDataWithBaseURL("android.resource://<package_name>/assets/www/file_name", html, mimeType, encoding, "");
like image 37
Shankar Agarwal Avatar answered Nov 11 '22 20:11

Shankar Agarwal