开发者

Out of memory, showing AlertDialog and making changes in orientation

I have an activity containing two imagebuttons. If the user clicks back an alertDialog is shown. While the alert is shown, if the orientation is changed back and forth a few times the application crashes with this message:

ERROR/dalvikvm-heap(10988): 3363556-byte external allocation too large for this process.
ERROR/dalvikvm(10988): Out of memory: Heap Size=4935KB, Allocated=2594KB, Bitmap Size=19579KB ERROR/GraphicsJNI(10988): VM won't let us allocate 3363556 bytes

If the AlertDialog is not there, the application doesn't crash.

public class StartPageView extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener
{
    private static final String TAG = "StartPageView";
    private ImageButton vacButton;
    private ImageButton sickButton;
    private AlertDialog alert;
    private static boolean alertDismissedByLifeCycle = false;


@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.startpage);

    vacButton = (ImageButton)findViewById(R.id.gotovac_btn);
    vacButton.setOnClickListener(this);

    sickButton = (ImageButton)findViewById(R.id.gotosick_btn);
    sickButton.setOnClickListener(this); 

    if(alertDismissedByLifeCycle)
    {
        alertDismissedByLifeCycle = false;
        showAlert();
    }
}

@Override
protected void onStop() 
{
    if(alert != null && alert.isShowing())
    {
        alertDismissedByLifeCycle = true;
        alert.dismiss();
    }
    super.onStop();
}

//GO TO VACATION VIEW OR SICK VIEW

//user clicked on vacView button or sickView button
@Override
public void onClick(View v) 
{
    if(v.equals(this.vacButton))
    {
         Intent i = new Intent(this, VacationLeaveView.class);
         startActivity(i);
    }
    if(v.equals(this.sickButton))
    {
         Intent i = new Intent(this, SickLeaveView.class);
         startActivity(i);

    }

}


//LOG OUT

//user pressed the back button
@Override
public void onBackPressed() 
{
    showAlert();
}
//OnClickListener for logging out warning
@Override
public void onClick(DialogInterface dialog, int which) 
{
    if(which == DialogInterface.BUTTON_POSITIVE)
    {
        MainController.getInstance().clearAllChildren();
    开发者_如何学运维    dialog.cancel();
        this.finish();
    }
    else
        dialog.cancel();
}

//DIALOG

private void showAlert()
{
    if(alert == null)
    {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        alertDialog.setMessage("Vill du logga ut från förskoleportalen?");
        alertDialog.setPositiveButton("Ja", this);
        alertDialog.setNegativeButton("Nej", this);

        alert = alertDialog.create();

        // Title for AlertDialog
        alert.setTitle("Logga ut");
    }

    alert.show();
}


It clearly indicates you, that you're using a very big sized bitmap, you just need to convert it to weigh less.


Your context of "this" is almost certainly the cause. You need to explicitly specify context to avoid this issue. Your very issue is discussed here: Avoiding Memory Leaks

The problem is this ... when you add your images inside the activity to something with the context of "this" and then change the orientation the image gets passed along with everything else that is "this" to the onCreate() method as part of the bundle. Do that over and over again and you actually end up with 1 image per orientation change in memory. The Android documentation speaks to this verbatim.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜