2

Today I got my app crashed, Out of memory error.

java.lang.OutOfMemoryError
in android.graphics.BitmapFactory.nativeDecodeAsset

I only used Bitmap Factory to make a backgroud to my action bar

The code:

BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.drawable.actionbar)); 
         background.setTileModeX(android.graphics.Shader.TileMode.REPEAT); 
         actionbar.setBackgroundDrawable(background);

This error doesn't happend on activity start, it happens after changing in activites a lot.

Can someone show me how to fix this

EDIT EDIT EDIT

Here is the error message in developer console:

java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3838)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3833)
... 11 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
at android.content.res.Resources.loadDrawable(Resources.java:2988)
at android.content.res.Resources.getDrawable(Resources.java:1558)
at android.widget.ImageView.resolveUri(ImageView.java:646)
at android.widget.ImageView.setImageResource(ImageView.java:375)
at com.packagename.pp.Activitytwo.disableAnswer(Activitytwo.java:435)
at com.packagename.pp.Activitytwo.submitAnswer(Activitytwo.java:230)
... 14 more
Sam M
  • 159
  • 1
  • 3
  • 12
  • I posted the solution to this problem [here][1]. [1]: https://stackoverflow.com/questions/25344518/outofmemory-exception-not-sure-android/25344553#25344553 – Jose Rodriguez Aug 27 '14 at 17:22
  • This may suggest you have a memory leak somewhere in your code, ie. you're not freeing the previously allocated bitmap, and something still holds the reference to it. – Jitsu Aug 27 '14 at 17:23
  • @Joseph I don't understand what do you mean – Sam M Aug 27 '14 at 17:27
  • @Jitsu yes, but I don't know how to fix it – Sam M Aug 27 '14 at 17:27

2 Answers2

5

You have a memory leak on your code. Consider the use of WeakReference, WeakHashMap or SoftReference to avoid strong references. Free unused resources and variables on onLowMemory method of activities.

Also you can use the option BitmapFactory.Options to decode the bitmap as show the examples here.

I posted the some hints to solve this problem here.

Jose Rodriguez
  • 9,753
  • 13
  • 36
  • 52
0

first try commenting this code and see if that alone is the issue. It could be something else that might be using up the memory.