我怎样才能在android中修复InvocationTargetException,OutOfMemoryError和IllegalStateException?

时间:2016-09-08 18:00:56

标签: android memory-management crash imageview out-of-memory

我调用此函数为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) 

如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

我更改了以下代码

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

smileysImg.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.smiley_1, null)) 我也做了这个

android:largeHeap="true"

没有解决问题。应用程序仍然崩溃,但它花了比以前更多的时间。 不正确缩放图像将导致OutofMemoryError,但我的图像很小,所以我认为没有必要缩小它们。

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