android WebView上有空白/白色屏幕
首先,我想说的是,我可以在Stack Overflow和其他很多地方看到很多与我的问题类似的问题。然而,我仍然无法从他们那里找到解决问题的方法,所以我把我的问题放在这里 我正在编写一个带有WebView的应用程序,当它加载URL时,我会看到一个白色的空白屏幕。它工作得很好,以前加载过网页。但是现在它只显示一个白色屏幕,似乎没有加载页面。当我给出错误的URL时,它甚至没有给出在android WebView上有空白/白色屏幕,android,android-studio,webview,Android,Android Studio,Webview,首先,我想说的是,我可以在Stack Overflow和其他很多地方看到很多与我的问题类似的问题。然而,我仍然无法从他们那里找到解决问题的方法,所以我把我的问题放在这里 我正在编写一个带有WebView的应用程序,当它加载URL时,我会看到一个白色的空白屏幕。它工作得很好,以前加载过网页。但是现在它只显示一个白色屏幕,似乎没有加载页面。当我给出错误的URL时,它甚至没有给出在onReceivedError()方法中定义的消息。目前,我正在用一个带有android API 22的虚拟设备进行测试。
onReceivedError()
方法中定义的消息。目前,我正在用一个带有android API 22的虚拟设备进行测试。下面是我的代码。有人能告诉我错误是什么以及如何修复它吗
主要活动
package com.test.testApp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private static final String URL = "https://www.google.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(URL);
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
public class MyWebViewClient extends WebViewClient {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
String htmlData = "<html><body><div align=\"center\">Something wrong happened. Please check your internet connection and try again...!</div></body>";
webView.loadUrl("about:blank");
webView.loadDataWithBaseURL(null, htmlData, "text/html", "UTF-8", null);
}
}
}
package com.test.testApp;
导入androidx.appcompat.app.appcompat活动;
导入android.os.Bundle;
导入android.view.Window;
导入android.webkit.WebSettings;
导入android.webkit.WebView;
导入android.webkit.WebViewClient;
公共类MainActivity扩展了AppCompatActivity{
私有网络视图;
私有静态最终字符串URL=”https://www.google.com/";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
requestWindowFeature(窗口。功能\u无\u标题);
getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=(webView)findviewbyd(R.id.webView);
setWebViewClient(新的MyWebViewClient());
WebSettings WebSettings=webView.getSettings();
setJavaScriptEnabled(true);
loadUrl(URL);
}
@凌驾
public void onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
}否则{
super.onBackPressed();
}
}
公共类MyWebViewClient扩展了WebViewClient{
@凌驾
public void onReceivedError(WebView视图、int错误代码、字符串描述、字符串失败URL){
String htmlData=“发生了错误。请检查您的internet连接,然后重试…!”;
loadUrl(“关于:空白”);
loadDataWithBaseURL(null,htmlData,“text/html”,“UTF-8”,null);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layerType="hardware">
</WebView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testApp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity">
<intent-filter>
<action android:name="com.test.testApp.SettingsActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
试试看
webView.getSettings().setDomStorageEnabled(true)
在清单中尝试此操作可能是您的问题应该得到解决的原因
android:usesCleartextTraffic="true"
我只需更新Android Studio和AVD就可以解决这个问题。不知道真正的原因是什么。不过,如果其他人面临这个奇怪的问题,我认为最好也尝试更新Android studio和AVD。谢谢您的回复。但它不起作用。这个属性只有在android API等级23之后才允许。但是,我的应用程序支持API级别21。请将您的API级别更改为23,然后检查其是否正常工作