Raj
Raj

Reputation: 693

Problem with webview not loading

I am having a bad experience with webview which does not load the web page which I request.

I cannot load google or any other page with a webview. I have put in xml:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_marginLeft="250px"
         android:layout_marginTop="80px"
    android:layout_width="180px"
    android:layout_height="160dip"
/>

I then put in the code:

mWebView = (WebView) findViewById(R.id.webview);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.loadUrl("http://www.google.com");

This shows up stating the webpage is not available.

I have also added the permission to the manifest.

I have another activity within this application which loads a youtube url fine using:

startActivity(new Intent( Intent.ACTION_VIEW,
                                Uri.parse("http://www.youtube.com/watch?v=XS998HaGk9M")));// Starts an intent to watch the video

I'm not sure what this could be and really need advice on this as I need to get it working.

Thanks

Edit: I also cannot access any webpage within the actual emulator itself. By searching in the search bar within the emulator this says the same thing when connecting to Google.

I'm not sure why this would connect to youtube with an intent and not a webview

Edit: This is not even connecting to youtube now, it says the same as above. This is messed up as I need this to work for my project tomorrow. If the webview keeps going down this is not very reliable. I may have to change the device I'm working with as with android things keep going wrong.

Edit: I have just come back after a few hours without touching the code or the emulator and when I run the application the youtube video was back on and I can browse within the emulator. But I still cannot connect via webview. VERY UNRELIABLE :(

Upvotes: 6

Views: 5758

Answers (4)

Apk
Apk

Reputation: 153

 package com.Example.Browser;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.KeyEvent;
 import android.view.View;
 import android.webkit.WebSettings;
 import android.webkit.WebSettings.PluginState;
 import android.webkit.CookieManager;
 import android.webkit.WebView;
 import android.webkit.WebSettings.RenderPriority;
 import android.webkit.WebViewClient;

 public class MainActivity extends Activity {


private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CookieManager.getInstance().setAcceptCookie(true);//Enable Cookies

    mWebView = (WebView) findViewById(R.id.webView1);
    mWebView.getSettings().setJavaScriptEnabled(true);//Enable Java Script
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.loadUrl("http://www.google.com/"); //Set Home page
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//Remove ScrollBars
    mWebView.getSettings().setDefaultFontSize(12);//Set Font Size
    mWebView.getSettings().setLoadsImagesAutomatically(true);//Enable Image Loading
    mWebView.getSettings().setPluginState(PluginState.ON);//Enable Flash
    mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); //improves Feedback     on touch
    //mWebView.setBackgroundColor(0x00000000);//Transparent Screen When Loading
    //mWebView.getSettings().setBuiltInZoomControls(true);//Set Zoom Controls 
    //mWebView.getSettings().setDisplayZoomControls(false);//Always Hide Zoom     Controlls(Requires Api 11)

    mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);//Set Cache (8mb)
    String appCachePath =     getApplicationContext().getCacheDir().getAbsolutePath();//Set Cache (8mb)
    mWebView.getSettings().setAppCachePath(appCachePath);//Set Cache (8mb)
    mWebView.getSettings().setAllowFileAccess(true);//Set Cache (8mb)
    mWebView.getSettings().setAppCacheEnabled(true);//Set Cache (8mb)
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//Set Cache (8mb)

    mWebView.requestFocus(View.FOCUS_DOWN);//Enable WebView Interaction

    //mWebView.setWebViewClient(new WebViewClient() {//Open URL on Error
    //public void onReceivedError(WebView view, int errorCode, String description,     String failingUrl) {//Open URL on Error   
    //mWebView.loadUrl("http://www.google.com");//Open URL on Error 

    //mWebView.loadUrl("file:///android_asset/error_404.jpg"); //Show Offline HTML     file or Image on Error 
    //  }
    // });
  }

private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{


webview.loadUrl(url);
return true;
}
}

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {

 if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())

{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

 <WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

Upvotes: 1

Manu
Manu

Reputation: 21

I had the same problem. It seems to be solved when putting the webview code elsewhere than in the MainActivity.onCreate() method . For example, put the code containing webview.loadUrl(…) into the onClick method of a button. The WebView appears empty on launching the activity, and correctly filled when clicking the button .

Upvotes: 2

Brighton Vino
Brighton Vino

Reputation: 395

Make sure you have included Permissions for Internet Access

<uses-permission android:name="android.permission.INTERNET" />

Upvotes: 3

Dan
Dan

Reputation: 3884

Does the emulator have internet access? I have noticed similar behavior within the emulator at times and it is due to the emulator not starting up properly. The only work-a-round I've been able to come up with is to restart the emulator until it has internet access (usually one or two times).

-Dan

Upvotes: 1

Related Questions