“活动已泄露最初添加的窗口”

时间:2013-12-24 06:07:30

标签: android memory-leaks android-alertdialog

以下是我的代码,

public class Assignment_Create extends Activity implements OnClickListener {

DataManipulator dataManipulator;
static final int DIALOG_ID = 1;
AlertDialog.Builder dialogAc;

ImageView imageViewDateAssign, imageViewDueDate, imageViewSubmit;
TextView textViewDtAssign, textViewDueDt;
EditText editTextTitle, editTextDesc;

static final int DATE_DIALOG_ID = 0;
int cDay, cMonth, cYear;

private TextView activeDateDisplay;
private Calendar activeDate;

// Update database
String updateId;
public boolean isEdit;

@Override
public void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.assignment_create);

    imageViewDateAssign = (ImageView) findViewById(R.id.dateassign);
    imageViewDueDate = (ImageView) findViewById(R.id.duedate);
    imageViewSubmit = (ImageView) findViewById(R.id.submit);

    textViewDtAssign = (TextView) findViewById(R.id.textViewDateAssign);
    textViewDueDt = (TextView) findViewById(R.id.textViewDueDate);

    editTextTitle = (EditText) findViewById(R.id.title);
    editTextDesc = (EditText) findViewById(R.id.description);

    isEdit = getIntent().getExtras().getBoolean("isEdit");
    updateId = getIntent().getExtras().getString("idNo");

    if (isEdit) {
        editTextTitle.setText(getIntent().getExtras().getString(
                "AsmntTitle"));
        editTextDesc
                .setText(getIntent().getExtras().getString("AsmntDesc"));
    }

    Code.AssignDate = Calendar.getInstance();
    Code.DueDate = Calendar.getInstance();

    imageViewDateAssign.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            showDateDialog(textViewDtAssign, Code.AssignDate);
        }
    });

    imageViewDueDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            showDateDialog(textViewDueDt, Code.DueDate);
        }
    });

    imageViewSubmit.setOnClickListener(this);

    updateDisplay(textViewDtAssign, Code.AssignDate);
    updateDisplay(textViewDueDt, Code.DueDate);
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.submit:

        Code.title = editTextTitle.getText().toString().trim();
        Code.description = editTextDesc.getText().toString().trim();
        Code.diff = Code.DueDate.getTimeInMillis()
                - Code.AssignDate.getTimeInMillis();
        Code.days = Code.diff / (24 * 60 * 60 * 1000);
        Code.strDays = String.valueOf(Code.days);

        if (isEdit) {
            this.dataManipulator = new DataManipulator(this);
            this.dataManipulator.update(updateId);
            this.dataManipulator.close();
        } else {
            this.dataManipulator = new DataManipulator(this);
            this.dataManipulator.insert(Code.title, Code.description,
                    Code.strDays);
            this.dataManipulator.close();
        }

        if (Code.title.length() <= 0 || Code.description.length() <= 0) {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    Assignment_Create.this);
            alertDialogBuilder.setTitle("Your Title");
            alertDialogBuilder
                    .setMessage("Click yes to exit!")
                    .setCancelable(false)
                    .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.cancel();
                                }
                            });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();

            Toast.makeText(getApplicationContext(), "Not proper date",
                    Toast.LENGTH_LONG).show();
        }

        Toast.makeText(getApplicationContext(),
                "Details are saved successfully", Toast.LENGTH_LONG).show();
        Toast.makeText(getApplicationContext(),
                "Assignment Created Succesfully", Toast.LENGTH_LONG).show();
        Assignment_Create.this.finish();
        break;
    }
}

private void updateDisplay(TextView dateDisplay, Calendar date) {
    dateDisplay.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(date.get(Calendar.MONTH) + 1).append("-")
            .append(date.get(Calendar.DAY_OF_MONTH)).append("-")
            .append(date.get(Calendar.YEAR)).append(" "));
}

@SuppressWarnings("deprecation")
public void showDateDialog(TextView dateDisplay, Calendar date) {
    activeDateDisplay = dateDisplay;
    activeDate = date;
    showDialog(DATE_DIALOG_ID);
}

private OnDateSetListener dateSetListener = new OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        activeDate.set(Calendar.YEAR, year);
        activeDate.set(Calendar.MONTH, monthOfYear);
        activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateDisplay(activeDateDisplay, activeDate);

        unregisterDateDisplay();
    }
};

private void unregisterDateDisplay() {
    activeDateDisplay = null;
    activeDate = null;
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, dateSetListener,
                activeDate.get(Calendar.YEAR),
                activeDate.get(Calendar.MONTH),
                activeDate.get(Calendar.DAY_OF_MONTH));
    }
    return null;
}

@SuppressWarnings("deprecation")
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);
    switch (id) {
    case DATE_DIALOG_ID:
        ((DatePickerDialog) dialog).updateDate(
                activeDate.get(Calendar.YEAR),
                activeDate.get(Calendar.MONTH),
                activeDate.get(Calendar.DAY_OF_MONTH));
        break;
    }
}
}

一切正常,但是当我将两个编辑文本都留空时,它应该显示警告对话框,但不是这样,它给出了Activity已泄露窗口的错误。

下面是我的logcat ......

12-24 11:32:55.995: E/WindowManager(1940): Activity iqualtech.skirr.Assignment_Create has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@420cf5b0 that was originally added here
12-24 11:32:55.995: E/WindowManager(1940): android.view.WindowLeaked: Activity iqualtech.skirr.Assignment_Create has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@420cf5b0 that was originally added here
12-24 11:32:55.995: E/WindowManager(1940):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
12-24 11:32:55.995: E/WindowManager(1940):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:268)
12-24 11:32:55.995: E/WindowManager(1940):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:216)
12-24 11:32:55.995: E/WindowManager(1940):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:141)
12-24 11:32:55.995: E/WindowManager(1940):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
12-24 11:32:55.995: E/WindowManager(1940):  at android.app.Dialog.show(Dialog.java:278)
12-24 11:32:55.995: E/WindowManager(1940):  at iqualtech.skirr.Assignment_Create.onClick(Assignment_Create.java:132)
12-24 11:32:55.995: E/WindowManager(1940):  at android.view.View.performClick(View.java:3517)
12-24 11:32:55.995: E/WindowManager(1940):  at android.view.View$PerformClick.run(View.java:14155)
12-24 11:32:55.995: E/WindowManager(1940):  at android.os.Handler.handleCallback(Handler.java:605)
12-24 11:32:55.995: E/WindowManager(1940):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-24 11:32:55.995: E/WindowManager(1940):  at android.os.Looper.loop(Looper.java:154)
12-24 11:32:55.995: E/WindowManager(1940):  at android.app.ActivityThread.main(ActivityThread.java:4624)
12-24 11:32:55.995: E/WindowManager(1940):  at java.lang.reflect.Method.invokeNative(Native Method)
12-24 11:32:55.995: E/WindowManager(1940):  at java.lang.reflect.Method.invoke(Method.java:511)
12-24 11:32:55.995: E/WindowManager(1940):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
12-24 11:32:55.995: E/WindowManager(1940):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-24 11:32:55.995: E/WindowManager(1940):  at dalvik.system.NativeStart.main(Native Method)

我尝试了以下一些链接

https://stackoverflow.com/search?q=activity+has+leaked+windows+in+android

但是我没有得到我需要的什么以及确切的地方。如果编辑文本都为空,我只想在提交按钮单击事件上显示警告对话框。任何帮助,将不胜感激。先感谢您。

3 个答案:

答案 0 :(得分:8)

您在致电Assignment_Create.this.finish();后直接致电dialog.show()。这意味着当您的活动被销毁时,对话框在屏幕上,这会导致泄露的窗口异常。您需要在致电dialog.dismiss()之前致电this.finish()关闭所有对话框。

您有两种选择:

  1. this.finish()移至您的onClickListene r内 警报对话框。这就是我认为你想要的,以便活动 只有当用户在对话框中单击“确定”时才会完成。
  2. 在致电alertdialog.finish()之前直接致电this.finish()

答案 1 :(得分:0)

Assignment_Create.this.finish(); 

上述声明将导致活动遭到破坏,但您的警报对话框仍处于打开状态。

您需要确保在销毁活动之前需要关闭警报对话框。这样就可以防止出现这种异常。

可能的解决方案:

  1. 您可以在活动的onDestroy()中关闭对话框。
  2. 我认为您希望在某些特定情况下完成,但目前完成对于R.id.submit中的所有情况都有效。

答案 2 :(得分:0)

删除Assignment_Create.this.finish();