xml文件中的@drawable导致应用程序崩溃

时间:2014-05-10 04:47:04

标签: android android-layout drawable android-drawable

我正在尝试通过android:background=@drawable/..向活动添加背景图片,如下面的代码所示。但问题是,当我添加此行android:background="@drawable/introbackground"时,应用程序崩溃,当我删除它时,应用程序正常工作。

要注意:我使用的图像,我已调整大小,以便适合整个屏幕。这可能是问题吗?

XML:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/introbackground"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.meetingpointlocator_03" 
>

LOGCAT OutPut:

05-10 08:06:24.101: D/dalvikvm(17707): GC_FOR_ALLOC freed 119K, 15% free 9922K/11580K, 
paused 15ms, total 15ms
05-10 08:06:24.101: I/dalvikvm-heap(17707): Forcing collection of SoftReferences for   
121925780-byte allocation
05-10 08:06:24.131: D/dalvikvm(17707): GC_BEFORE_OOM freed 9K, 15% free 9912K/11580K,  
paused 30ms, total 30ms
05-10 08:06:24.131: E/dalvikvm-heap(17707): Out of memory on a 121925780-byte   
allocation.
05-10 08:06:24.131: I/dalvikvm(17707): "main" prio=5 tid=1 RUNNABLE
05-10 08:06:24.131: I/dalvikvm(17707):   | group="main" sCount=0 dsCount=0 
obj=0x41c96578 self=0x41c1ca60
05-10 08:06:24.131: I/dalvikvm(17707):   | sysTid=17707 nice=-8 sched=0/0 cgrp=apps   
handle=1074503676
05-10 08:06:24.131: I/dalvikvm(17707):   | state=R schedstat=( 107360003 8309669 119 ) 
utm=6 stm=4 core=1
05-10 08:06:24.131: I/dalvikvm(17707):   at      
android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-10 08:06:24.131: I/dalvikvm(17707):   at  
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-10 08:06:24.131: I/dalvikvm(17707):   at 
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-10 08:06:24.131: I/dalvikvm(17707):   at 
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
05-10 08:06:24.131: I/dalvikvm(17707):   at 
android.content.res.Resources.loadDrawable(Resources.java:2988)
05-10 08:06:24.131: I/dalvikvm(17707):   at  
android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.view.View.<init>(View.java:3563)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.view.ViewGroup.<init>
(ViewGroup.java:475)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.widget.LinearLayout.<init>  
(LinearLayout.java:176)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.widget.LinearLayout.<init> 
(LinearLayout.java:172)
 05-10 08:06:24.131: I/dalvikvm(17707):   at     
 java.lang.reflect.Constructor.constructNative(Native Method)
05-10 08:06:24.131: I/dalvikvm(17707):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.view.LayoutInflater.createView(LayoutInflater.java:600)
05-10 08:06:24.131: I/dalvikvm(17707):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
05-10 08:06:24.131: I/dalvikvm(17707):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.app.Activity.setContentView(Activity.java:1956)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
05-10 08:06:24.131: I/dalvikvm(17707):   at com.example.meetingpointlocator_03.Intro.onCreate(Intro.java:38)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.app.Activity.performCreate(Activity.java:5372)
05-10 08:06:24.131: I/dalvikvm(17707):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)

2 个答案:

答案 0 :(得分:0)

  

121925780字节分配时内存不足。

你的应用内存不足。

  

要注意:我使用的图像,我已经调整了大小,以便它适合   整个屏幕。这可能是问题吗?

是的,可能的原因是这个背景图片需要按比例缩小。每个应用程序都分配了一定的堆大小。从android 3.0及以上位图像素数据存储在堆中。您需要缩小图像。

Out of Memory error with Bitmap

同时阅读将缩小版本加载到内存中

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

答案 1 :(得分:0)

您正在获取OOM,即内存不足以及

的充气器异常
android:background="@drawable/introbackground"

因为图片 introbackground 的尺寸非常大。

解决方案

 Use a .jpeg light weight image insted of using .png heavy image.