首先,我想说在Stack Overflow和许多其他地方都可以看到很多类似于我的问题的问题。然而,我仍然无法从它们中找到解决我的问题的方法,因此我将我的问题放在这里。
我正在编写一个带有WebView的应用程序,在加载URL时出现白屏。它之前工作得很好并且加载了网页。但是现在它只给了一个空白屏幕,并且似乎没有加载页面。甚至在我提供错误的URL时,它也不会给出
主要活动
我正在编写一个带有WebView的应用程序,在加载URL时出现白屏。它之前工作得很好并且加载了网页。但是现在它只给了一个空白屏幕,并且似乎没有加载页面。甚至在我提供错误的URL时,它也不会给出
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);
}
}
}
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>