簡體   English   中英

無法銷毀活動 Android

[英]unable to destroy activity 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", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            finish();
        }
    });

當我單擊后退按鈕時應用程序崩潰..當我調試它顯示的代碼java.lang.RuntimeException: Unable to destroy ActivityHVDactivities.CreateCollageActivity.onDestroy(CreateCollageActivity.java:740)

您應該在 super.onDestroy(); 之前編寫所有代碼; 將此行作為該方法的最后一條語句。

只需用onBackPressed()替換onDestroy()方法

@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", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            finish();
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM