【发布时间】:2019-09-20 12:55:39
【问题描述】:
我正在将 Android 应用从 API 27 更新到 API 29,我注意到在基于 5.0 和/或 5.1 的模拟器上尝试渲染 WebView 时出现崩溃。在运行 6.0 或更高版本 (API 23-29) 的模拟器上不会发生此问题。
我似乎找不到任何有关会影响 5.0 或 5.1 的 WebView 行为的文档,但我可以确认当我使用 API 27 运行应用程序时问题不会发生。我不知所措,因为我不知道是否这是一个模拟器问题或实际的 API/设备问题(我认为是后者)。
问题是 Activity 和 Fragment 根本不会膨胀,因为缺少 String 资源。这是一些堆栈跟踪(它似乎找不到字符串资源):
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MyActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.MyWebView
Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.MyWebView...
Caused by: java.lang.reflect.InvocationTargetException...
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040002
在崩溃发生之前,与资源相关的日志中有一些相关警告:
W/chromium: [WARNING:resource_bundle.cc(315)] locale_file_path.empty()
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
W/chromium: [WARNING:proxy_service.cc(901)] PAC support disabled because there is no system implementation
W/chromium: [WARNING:data_reduction_proxy_settings.cc(403)] SPDY proxy OFF at startup
W/ResourceType: No known package when getting value for resource number 0x02040002
网络视图:
public class MyWebView extends WebView {
public MyWebView (Context context) {
super(context);
initialize();
}
public MyWebView (Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public MyWebView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}
public MyWebView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize();
}
public MyWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
super(context, attrs, defStyleAttr, privateBrowsing);
initialize();
}
private void initialize() {
this.clearCache(true);
this.clearHistory();
this.getSettings().setJavaScriptEnabled(true);
this.getSettings().setLoadsImagesAutomatically(true);
this.getSettings().setUseWideViewPort(true);
this.getSettings().setAllowContentAccess(false);
this.getSettings().setAllowFileAccess(false);
this.getSettings().setAllowFileAccessFromFileURLs(false);
this.getSettings().setAllowUniversalAccessFromFileURLs(false);
this.getSettings().setDomStorageEnabled(false);
this.getSettings().setAppCacheEnabled(false);
this.getSettings().setDatabaseEnabled(false);
this.getSettings().setGeolocationEnabled(false);
this.getSettings().setSaveFormData(false);
this.getSettings().setSupportMultipleWindows(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.getSettings().setSafeBrowsingEnabled(false);
}
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.example.MyWebView
android:id="@+id/my_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
有什么想法我做错了什么,或者 API 28 或 29 中可能发生的变化可能会破坏这一点吗?
【问题讨论】:
-
我也遇到了同样的问题,但这是一个重复的问题。
-
哇,我在运行 Android 5.0.2 的模拟器时遇到了完全相同的崩溃,但运行 Android 5.1 的物理设备运行时没有任何问题。正如上面发布的@DmitryBrant 的答案,问题不在于 API 28 或 29,而在于
androidx.appcompat:appcompat:1.1.0,如果您降级到1.0.2或1.1.0-beta01,那么崩溃应该会消失。 -
@Seven 你确定它在物理设备上工作正常吗?因为我没有 API 级别 21 的物理设备可以测试!
-
@SumitShukla 是的,我确定。它在我运行 Android 5.1(Moto G 1st Gen)的物理设备上运行良好。