0

I have problem I do not understand. When I reach the following code snippet

public class MatchViewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("OnCreate in MatchViewActivity","1");
        setContentView(R.layout.activity_match_view); <--- Crash occurrs here
        Log.d("","2");
        Intent intent = getIntent();

I get the following error in the simulator, always:

10-07 18:41:39.012: E/dalvikvm-heap(1099): Out of memory on a 3686416-byte allocation.

Some lines further down in the logcat I can find:

 10-07 18:41:39.452: E/AndroidRuntime(1099): FATAL EXCEPTION: main
10-07 18:41:39.452: E/AndroidRuntime(1099): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.matchView.MatchViewActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.os.Looper.loop(Looper.java:137)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at java.lang.reflect.Method.invokeNative(Native Method)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at java.lang.reflect.Method.invoke(Method.java:511)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at dalvik.system.NativeStart.main(Native Method)
10-07 18:41:39.452: E/AndroidRuntime(1099): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.app.Activity.setContentView(Activity.java:1881)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at com.example.matchView.MatchViewActivity.onCreate(MatchViewActivity.java:25)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.app.Activity.performCreate(Activity.java:5104)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-07 18:41:39.452: E/AndroidRuntime(1099):     ... 11 more
10-07 18:41:39.452: E/AndroidRuntime(1099): Caused by: java.lang.reflect.InvocationTargetException
10-07 18:41:39.452: E/AndroidRuntime(1099):     at java.lang.reflect.Constructor.constructNative(Native Method)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
10-07 18:41:39.452: E/AndroidRuntime(1099):     ... 23 more
10-07 18:41:39.452: E/AndroidRuntime(1099): Caused by: java.lang.OutOfMemoryError
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.content.res.Resources.loadDrawable(Resources.java:1965)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.view.View.<init>(View.java:3330)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.view.ViewGroup.<init>(ViewGroup.java:431)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.widget.FrameLayout.<init>(FrameLayout.java:101)
10-07 18:41:39.452: E/AndroidRuntime(1099):     at android.widget.FrameLayout.<init>(FrameLayout.java:97)
10-07 18:41:39.452: E/AndroidRuntime(1099):     ... 26 more
10-07 18:46:39.632: I/Process(1099): Sending signal. PID: 1099 SIG: 9

I checked that "activity_match_view" does exist. What is happening here?

The corresponding layout file:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_background" >

    <ListView
        android:id="@+id/matchView_ListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:cacheColorHint="#00000000">
    </ListView>

</FrameLayout>
toom
  • 12,864
  • 27
  • 89
  • 128

3 Answers3

3

The error is because the bitmap that the OS is trying to create for your background is possibly way too huge, hence it runs out of resources, also make sure the memory delegated for the emulator has a decent value, other way it might run out of memory easily.

Hope this Helps.

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
  • Thanks, but I don't think so because this activity uses the same background image as the activity before – toom Oct 07 '13 at 18:59
  • yes, my point..., maybe the bitmap is too big, do this. Remove your background bmp, and change it for a single color and see what happens... – Martin Cazares Oct 07 '13 at 19:00
  • Yes, you're right. When the background image is removed from the xml it does not crash. So it is definitly due to the graphic. Additionally I extended the available RAM for the virtual device. Now it works. However the graphic is just 190kbyte. – toom Oct 07 '13 at 19:48
  • 2
    Yes, there's a dark side of android :P regarding that, it doesn't matter if the image size is 1KB, android internally takes the memory of width * height * 4Bytes of the image in order to use it, you can actually notice using the DDMS "heap tracker" that a huge Image can easily take couple of MBs even when stored is couple of KBs, If this answer was helpful i would appreciate if you either +1 or mark it as correct...Regards! – Martin Cazares Oct 07 '13 at 20:29
0

How big is your @drawable/main_background It could be to big to load. Every Bitmap is in your memory so please provide small pictures

A.S.
  • 4,574
  • 3
  • 26
  • 43
0

Use .jpeg/.jpg format for background images instead of .png images because on recreation of resources in android .png takes more heap memory as compare to .jpeg/.jpg images.

RatneZ
  • 1,078
  • 9
  • 9