内存不足,显示AlertDialog并进行方向更改

时间:2011-05-11 12:23:54

标签: android out-of-memory alertdialog screen-orientation

我有一个包含两个图像按钮的活动。如果用户单击后退,则会显示alertDialog。显示警报时,如果方向来回更改几次,则应用程序会因此消息而崩溃:

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

如果没有AlertDialog,则应用程序不会崩溃。

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

2 个答案:

答案 0 :(得分:1)

它清楚地表明你,你正在使用一个非常大的位图,你只需要将其转换为重量更轻。

答案 1 :(得分:0)

你对“这个”的背景几乎肯定是原因。您需要明确指定上下文以避免此问题。您的问题在此处讨论:Avoiding Memory Leaks

问题在于......当您将活动内的图像添加到具有“this”上下文的内容时,然后更改图像将其与“this”的所有其他内容一起传递给onCreate()方法作为捆绑的一部分。一遍又一遍地做到这一点,你实际上最终会在内存中每个方向更改一个图像。 Android文档会逐字说明。