简体   繁体   中英

unable to destroy activity Android

here is my java code

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

The app crashes when i click back button..when i debugged the code it showed java.lang.RuntimeException: Unable to destroy activity and HVDactivities.CreateCollageActivity.onDestroy(CreateCollageActivity.java:740)

You should write all the code before super.onDestroy(); Make this line the last statement of the method.

Just replace onDestroy() method with onBackPressed()

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

So your code will be like that

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM