【问题标题】:App crashes on adding image view添加图像视图时应用程序崩溃
【发布时间】:2016-03-18 05:48:08
【问题描述】:

我的应用在 11 个图像视图中运行良好。但是在向其添加另外 1 个图像视图时,应用程序崩溃了。我在堆栈溢出本身中找到了一些与此问题类似的答案,但找不到我的确切解决方案。

主要活动:

    package com.trendsetter.vyshnav.eatutor;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;


    public class BreakfastActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_breakfast);
}

    }

XML 文件:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i1" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d1a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d1b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t2"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:paddingTop="10dp"
        android:textColor="#006600"
        android:paddingBottom="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i2" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d2"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t3"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i3" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d3a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d3b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t4"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i4" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d4a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d4b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t5"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i5" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d5a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d5b"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t6"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i6" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d6a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d6b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t7"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i7" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d7a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d7b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t8"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i8" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d8a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d8b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t9"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i9" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d9a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d9b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t10"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i10" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d10a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d10b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t11"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i11" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d11a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d11b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</LinearLayout>
    </ScrollView>

Logcat:

    Caused by: java.lang.OutOfMemoryError: Failed to allocate a 9815052 byte allocation with 4194208 free bytes and 5MB until OOM

【问题讨论】:

  • 请张贴你的logcat。
  • @drawable/i3 从图片名称中删除 3 数字
  • @IntelliJAmiya 这不会导致崩溃
  • 原因:java.lang.OutOfMemoryError:无法分配 9815052 字节分配,4194208 字节和 5MB 直到 OOM
  • 我无法提供代码,因为我不知道您想要实现什么,而且我没有您的图片。

标签: android performance android-layout imageview


【解决方案1】:

发生这种情况是因为您的图像大小大于 2Mb,因此请尝试在您的 baseAdapter 中压缩图像: 你可以点击这个链接来压缩图像: How to compress image size?

【讨论】:

    【解决方案2】:

    添加 AndroidManifest.xml 文件并解决问题:

    <application
                 ....
                 ....
                 android:largeHeap="true"
    
     </application>
    

    【讨论】:

    • 您的图片大小超过 2MB 或更高
    • 请阅读上面的cmets。 “是的,我试过了,但仍然存在错误”
    【解决方案3】:

    您的 Scrollview 似乎是多组 3 个 TextView 和一个 ImageView 的集合。

    我建议使用 3 个字符串值和一个 int 值定义一个自定义对象,该值将保存图像的 R.drawable 整数值。

    然后,您可以定义一个自定义 XML 行单个对象。并通过 ListView + ArrayAdapter 或 RecyclerView 将每个对象“绑定”到视图。

    为了修复内存不足错误,您应该使用像 Picasso 或 Glide 这样的图像加载库将前面提到的整数资源 id 加载到 ImageView 中,而不是将完整的原始图像加载到内存中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      相关资源
      最近更新 更多