无法破坏Android活动

问题描述

这是我的Java代码

    @Override
    protected void onDestroy() {
    int i;
    super.onDestroy();
    if (bitmapList != null) {
        for (i = INDEX_COLLAGE; i < bitmapList.length; i += INDEX_COLLAGE_BACKGROUND) {
            if (bitmapList[i] != null) {
                bitmapList[i].recycle();
            }
        }
    }
    if (collageView != null) {
        if (collageView.shapeLayoutList != null) {
            for (i = INDEX_COLLAGE; i < collageView.shapeLayoutList.size(); i += INDEX_COLLAGE_BACKGROUND) {
                for (int j = INDEX_COLLAGE; j < collageView.shapeLayoutList.get(i).shapeArr.length; j += INDEX_COLLAGE_BACKGROUND) {
                    if (collageView.shapeLayoutList.get(i).shapeArr[j] != null) {
                        collageView.shapeLayoutList.get(i).shapeArr[j].freeBitmaps();
                    }
                }
            }
        }
        if (collageView.maskBitmapArray != null) {
            for (i = INDEX_COLLAGE; i < collageView.maskBitmapArray.length; i += INDEX_COLLAGE_BACKGROUND) {
                if (collageView.maskBitmapArray[i] != null) {
                    if (!collageView.maskBitmapArray[i].isRecycled()) {
                        collageView.maskBitmapArray[i].recycle();
                    }
                    collageView.maskBitmapArray[i] = null;
                }
            }
        }
    }
    if (adWhirlLayout != null) {
        adWhirlLayout.removeAllViews();
        adWhirlLayout.destroy();
    }
}
private void backButtonAlertBuilder() {
    AlertDialog.Builder builder = new AlertDialog.Builder(CreateCollageActivity.this);
    builder.setMessage("Would you like to save image ?").setCancelable(true).setPositiveButton("Yes",new OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            if (analytics != null)
                analytics.logEvent(Analytics.Param.IMAGE_SAVE,"");
            new SaveImageTask().execute();
        }
}).setNeutralButton("No",int which) {

            finish();
        }
    });

当我单击后退按钮时,该应用程序崩溃。.当我调试显示代码 java.lang.RuntimeException:无法破坏活动 HVDactivities.CreateCollageActivity.onDestroy(CreateCollageActivity.java :740)

解决方法

您应该在super.onDestroy();之前编写所有代码。将这一行作为该方法的最后一条语句。

,

只需将 onDestroy()方法替换为 onBackPressed()

@Override
public void onBackPressed() {
   super.onBackPressed();
}

所以您的代码将是这样

@Override
    protected void onBackPressed() {
    int i;
    super.onBackPressed();
    if (bitmapList != null) {
        for (i = INDEX_COLLAGE; i < bitmapList.length; i += INDEX_COLLAGE_BACKGROUND) {
            if (bitmapList[i] != null) {
                bitmapList[i].recycle();
            }
        }
    }
    if (collageView != null) {
        if (collageView.shapeLayoutList != null) {
            for (i = INDEX_COLLAGE; i < collageView.shapeLayoutList.size(); i += INDEX_COLLAGE_BACKGROUND) {
                for (int j = INDEX_COLLAGE; j < collageView.shapeLayoutList.get(i).shapeArr.length; j += INDEX_COLLAGE_BACKGROUND) {
                    if (collageView.shapeLayoutList.get(i).shapeArr[j] != null) {
                        collageView.shapeLayoutList.get(i).shapeArr[j].freeBitmaps();
                    }
                }
            }
        }
        if (collageView.maskBitmapArray != null) {
            for (i = INDEX_COLLAGE; i < collageView.maskBitmapArray.length; i += INDEX_COLLAGE_BACKGROUND) {
                if (collageView.maskBitmapArray[i] != null) {
                    if (!collageView.maskBitmapArray[i].isRecycled()) {
                        collageView.maskBitmapArray[i].recycle();
                    }
                    collageView.maskBitmapArray[i] = null;
                }
            }
        }
    }
    if (adWhirlLayout != null) {
        adWhirlLayout.removeAllViews();
        adWhirlLayout.destroy();
    }
}
private void backButtonAlertBuilder() {
    AlertDialog.Builder builder = new AlertDialog.Builder(CreateCollageActivity.this);
    builder.setMessage("Would you like to save image ?").setCancelable(true).setPositiveButton("Yes",new OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            if (analytics != null)
                analytics.logEvent(Analytics.Param.IMAGE_SAVE,"");
            new SaveImageTask().execute();
        }
}).setNeutralButton("No",int which) {

            finish();
        }
    });