Android 6 WebView 运行内存不足

标签 android webview scrollview android-6.0-marshmallow

我遇到了问题,在 SDK 23 之前一切都很好。但现在我收到以下错误:

W/art: Throwing OutOfMemoryError "Failed to allocate a 61819212 byte allocation with 4032304 free bytes and 3MB until OOM"     
W/JavaBrowserViewRendererHelper: Error allocating bitmap    
E/chromium: [ERROR:java_browser_view_renderer_helper.cc(133)] Error unlocking java bitmap pixels.

并且 WebView 未显示。这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/post_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:fillViewport="false"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollbars="vertical"
    android:touchscreenBlocksFocus="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical"
        android:touchscreenBlocksFocus="false">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/placeholder" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left|top"
            android:layout_marginBottom="30dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="30dp"
            android:gravity="center|center_horizontal"
            android:text="Title"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <WebView
            android:id="@+id/webView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/textView"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:scrollbars="none" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center" />

        <ImageView
            android:id="@+id/nextprev"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="30dp"
            android:adjustViewBounds="true"
            android:src="@drawable/nextprev" />
    </LinearLayout>
</ScrollView>

正如您所看到的,WebView 位于 ScrollView 中,因为我希望 WebView 上方和下方的 View 能够滚动。我还尝试过没有 ScrollView 的解决方案 Webview in Scrollview WebView 加载得很好,但是上面的内容都会滚动出来,但会留下空白区域覆盖 WebView。

我不知道,也许这只是模拟器故障?无论如何,我很想知道是什么原因造成的以及如何解决它。

最佳答案

我认为您已经解决了这个问题,但是为了回答您的问题,您的模拟器内存​​不足 (OOM)。

我在管理图像时遇到了同样的问题,所以我认为您可以减少应用程序中的图像数量。另外,如果您想正确管理它们,我建议您观看此视频https://www.youtube.com/watch?v=JTlJT2b5dd4

如有错误,请指正。

关于Android 6 WebView 运行内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33998536/

相关文章:

android - 如何在 Android Lollipop 中保存 WebView 状态并恢复它?

javascript - 如何使我的 .css 和 .js 文件与 Android 上的 WebView 中加载的 html 代码一起使用

android - 智能手机浏览器上的 SVG 支持

android - 数据库引用还是娱乐?

android api与谷歌地图显示网格

javascript - 如何在 WebView 中扩展 JavaScript 控制台(以匹配 ConsoleMessage.MessageLevel)

java - 在不丢失第一个排序顺序的情况下进行子排序?

android - Android ImageButton 背景的挑战

android - 嵌套滚动

android - 实现Android时间线日历的最佳方式