开发者

Android WebView shows a blank page

开发者 https://www.devze.com 2022-12-25 17:46 出处:网络
Trying to create a WebView but it only shows a blank/white page. I have followed several examples and they all say that work with this code...

Trying to create a WebView but it only shows a blank/white page. I have followed several examples and they all say that work with this code...

Here is my code:

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

public class PostenWebView extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.la开发者_如何学Pythonyout.web_view);
        WebView webview = (WebView)findViewById(R.id.webview);
        webview.loadUrl("http://www.google.com");
    }
}

And here is the web_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <WebView 
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
</LinearLayout>


You need to enable Javascript (.getSettings().setJavaScriptEnabled(true)), or choose a Web page that does not rely upon Javascript.


You have to add the permission to your AndroidManifest.xml file.

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


Use webview.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); method


It's works fine for me

WebView webView = (WebView)findViewById(R.id.webView);

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});

webView.loadUrl("http://www.google.com");

Good Luck!


You might be getting redirected.. just install a webviewclient allong with you web view :)


in my case loading google.com worked but my login page returned a completely white page. Adding this fixed it:

            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);

            webSettings.setDatabaseEnabled(true);
            webSettings.setDomStorageEnabled(true);
            String databasePath = webView.getContext().getDir("databases", Context.MODE_PRIVATE).getPath();
            webSettings.setDatabasePath(databasePath);


The quick and dirty solution can be when your android app get started set the background colour according to web view eg i am using black colour so myWebView.setBackgroundColor(Color.parseColor("#000000"));


if you try to load a HTTPS URL (e.g. Foursquare authentication URL) do not forget to call

webview.clearSslPreferences();

before trying to load that

0

精彩评论

暂无评论...
验证码 换一张
取 消