Question

I am trying to add a background image to the activity by android:background=@drawable/.. as shown in the code below. But the problem is, when i add this line android:background="@drawable/introbackground" the app crashs, and when i delete it the app works normally.

To note: the image i use, i have resized it so that, it fits the entire screen. is this could be the problem?

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)
Was it helpful?

Solution

Out of memory on a 121925780-byte allocation.

You app ran out of memory.

To note: the image i use, i have resized it so that, it fits the entire screen. is this could be the problem?

Yes the likely cause is this background image which needs to be scaled down. Each app is allocated a certain heap size. As of android 3.0 and above bitmap pixel data is stored in the heap. You need to scale down the image.

Out of Memory error with Bitmap

Also read Load a Scaled Down Version into Memory

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

OTHER TIPS

You are getting OOM i.e out of memory and also the inflator exception at

android:background="@drawable/introbackground"

because the image introbackground is of very large size.

Solution

 Use a .jpeg light weight image insted of using .png heavy image.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top