0

Im calling this function to set background image for an ImageView object "smileysImg" and play an animation "smileyFadeoutAnim". This is the code:

    public void showSmileyImage(){
    if(smileyNum > 5)
    smileyNum = 1;
    else smileyNum++;
    switch (smileyNum){
        case 1: smileysImg.setBackgroundResource(R.drawable.smiley_1); break;
        case 2: smileysImg.setBackgroundResource(R.drawable.smiley_2); break;
        case 3: smileysImg.setBackgroundResource(R.drawable.smiley_3); break;
        case 4: smileysImg.setBackgroundResource(R.drawable.smiley_4); break;
        case 5: smileysImg.setBackgroundResource(R.drawable.smiley_5); break;
        default: smileysImg.setBackgroundResource(R.drawable.smiley_5);
    }
    smileysImg.setVisibility(View.VISIBLE);

    smileyFadeoutAnim.setAnimationListener(new Animation.AnimationListener() 
    {
        @Override
        public void onAnimationStart(Animation animation) {
        }
        @Override
        public void onAnimationEnd(Animation animation) {
            smileysImg.setVisibility(View.GONE);
            smileysImg.clearAnimation();
            smileysImg.setBackgroundResource(0);
        }
        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });

    smileysImg.startAnimation(smileyFadeoutAnim);
}

It works some times and sometimes it crashes with this error log

FATAL EXCEPTION: main
                                                                                  Process: com.testaday.tad1stgrademathpractice, PID: 32399
                                                                                  java.lang.IllegalStateException: Could not execute method of the activity
                                                                                      at android.view.View$1.onClick(View.java:3823)
                                                                                      at android.view.View.performClick(View.java:4438)
                                                                                      at android.view.View$PerformClick.run(View.java:18422)
                                                                                      at android.os.Handler.handleCallback(Handler.java:733)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:136)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                      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:515)
                                                                                      at android.view.View$1.onClick(View.java:3818)
                                                                                      at android.view.View.performClick(View.java:4438) 
                                                                                      at android.view.View$PerformClick.run(View.java:18422) 
                                                                                      at android.os.Handler.handleCallback(Handler.java:733) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                      at android.os.Looper.loop(Looper.java:136) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                      at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                      at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                      at dalvik.system.NativeStart.main(Native Method) 
                                                                                   Caused by: java.lang.OutOfMemoryError
                                                                                      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
                                                                                      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
                                                                                      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
                                                                                      at android.content.res.Resources.loadDrawable(Resources.java:2115)
                                                                                      at android.content.res.Resources.getDrawable(Resources.java:700)
                                                                                      at android.view.View.setBackgroundResource(View.java:15303)
                                                                                      at com.testaday.tad1stgrademathpractice.ExamView.showSmileyImage(ExamView.java:836)
                                                                                      at com.testaday.tad1stgrademathpractice.ExamView.onClick_submit(ExamView.java:750)
                                                                                      at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                      at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                      at android.view.View$1.onClick(View.java:3818) 
                                                                                      at android.view.View.performClick(View.java:4438) 
                                                                                      at android.view.View$PerformClick.run(View.java:18422) 
                                                                                      at android.os.Handler.handleCallback(Handler.java:733) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                      at android.os.Looper.loop(Looper.java:136) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                      at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                      at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                      at dalvik.system.NativeStart.main(Native Method) 

how to fix this problem.

Sri
  • 23
  • 7

1 Answers1

0

I changed the following code

case 1: smileysImg.setBackgroundResource(R.drawable.smiley_1); break;

to

smileysImg.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.smiley_1, null)) and I also did this

android:largeHeap="true"

which didn't solve the issue. App still crashed but it took a bit more time than before. Not properly scaling the images will result in OutofMemoryError but my images are small so I thought there was no need to scale them down.

After a lot of searching, I found out from this answer that it is a good idea to call System.gc(); before calling my showSmileyImage(). It solved the problem. But i also found this answer which "probably" contradicts the first answer, but still calling gc solved the problem.

Community
  • 1
  • 1
Sri
  • 23
  • 7