gpt4 book ai didi

android - "Activity has leaked window that was originally added here"

转载 作者:太空狗 更新时间:2023-10-29 15:53:37 26 4
gpt4 key购买 nike

下面是我的代码,

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 has leaked window 的错误。

下面是我的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

但我不知道我需要更改什么以及确切的位置。如果两个编辑文本都为空,我只想在提交按钮单击事件上显示警报对话框。任何帮助,将不胜感激。先感谢您。

最佳答案

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

你有两个选择:

  1. this.finish() 移动到您的 onClickListener 中警报对话框。这就是我想你想要的,这样 Activity 仅当用户在对话框中单击“确定”时才会完成。
  2. 在调用 this.finish() 之前直接调用 alertdialog.finish()

关于android - "Activity has leaked window that was originally added here",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20755739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com