[英]Android WebView shows a blank page
試圖創建一個 WebView,但它只顯示一個空白/白頁。 我遵循了幾個示例,他們都說可以使用此代碼...
這是我的代碼:
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.layout.web_view);
WebView webview = (WebView)findViewById(R.id.webview);
webview.loadUrl("http://www.google.com");
}
}
這是 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>
您需要啟用 Javascript ( .getSettings().setJavaScriptEnabled(true)
),或選擇不依賴 Javascript 的網頁。
您必須將權限添加到您的 AndroidManifest.xml 文件中。
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
使用 webview.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); 方法
這對我來說很好用
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");
祝你好運!
我找到了一個終於奏效的例子! :)
但問題還在於URL,當我試圖通過我的WebView立即打開google.com時,立即在我的手機上打開我的普通瀏覽器...但其他網址的工作正常:)
您可能會被重定向.. 只需在您的網絡視圖中安裝一個 webviewclient :)
在我的情況下,加載 google.com 工作正常,但我的登錄頁面返回了一個完全白色的頁面。 添加此修復它:
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);
快速而骯臟的解決方案可能是當您的 android 應用程序開始時根據網絡視圖設置背景顏色,例如我使用黑色所以 myWebView.setBackgroundColor(Color.parseColor("#000000"));
如果您嘗試加載 HTTPS URL(例如 Foursquare 身份驗證 URL),請不要忘記調用
webview.clearSslPreferences();
在嘗試加載之前
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.