2016-03-18 141 views
-2

我的应用程序与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 
+2

发表您的logcat的请。 –

+0

'@ drawable/i3'从图像名称中删除'3'号码 –

+0

k..just一秒请按 – vvv

回答

0

它发生,因为你的图像尺寸大于2Mb的所以尝试在baseAdapter对图像进行压缩: 哟可以按照这个链接来压缩图像: How to compress image size?

+0

k tnx ...我会试试.. – vvv

0

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

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

</application> 
+0

已经尝试过。 –

+0

您的图片尺寸大于2MB或高于 –

+0

请阅读上面的注释。 “yhea我试过了,但仍然存在错误” –

0

您的滚动查看似乎是一组3个TextViews和一个ImageView的集合。

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

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

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

+0

谢谢..我会试试这个肯定.. – vvv

相关问题