我调用此函数来为ImageView对象设置背景图像“smileysImg”并播放动画“smileyFadeoutAnim”。这是代码:

    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);
}

它有时会工作,有时会因此错误日志而崩溃
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) 

如何解决这个问题。

最佳答案

我更改了以下代码
case 1: smileysImg.setBackgroundResource(R.drawable.smiley_1); break;

smileysImg.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.smiley_1, null))我也这样做了
android:largeHeap="true"
这并不能解决问题。 App仍然崩溃,但比以前花费了更多时间。
无法正确缩放图像会导致OutofMemoryError,但是我的图像很小,所以我认为没有必要缩小图像。

经过大量搜索,我发现from this answer是在调用System.gc();之前先调用showSmileyImage()的一个好主意。它解决了问题。
但是我也发现这个answer“可能”与第一个答案相矛盾,但是仍然调用gc解决了这个问题。

08-03 20:27