在显示确认关闭应用程序的对话框时,我被以下内容所困扰。该应用程序可以正常关闭,但在logcat整个页面是红色的。我已经通过网络进行了研究,有些人说应该创建对话框。
@Override
public void onBackPressed()
{
final Dialog dialog1 = new Dialog(First.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog1.setContentView(R.layout.alert_dialog_quit_with_email);
dialog1.setCancelable(true);
dialog1.show(); // LINE 323
TextView thankyou = (TextView) dialog1.findViewById(R.id.thankyou);
TextView Title_V1 = (TextView) dialog1.findViewById(R.id.Title_V1);
Button alert_cancel = (Button) dialog1.findViewById(R.id.button_cancel);
Button alert_quit = (Button) dialog1.findViewById(R.id.button_ok);
Button button_email = (Button) dialog1.findViewById(R.id.button_emailll);
alert_cancel.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
dialog1.dismiss();
return;
}
});
alert_quit.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
First.this.finish();
}
});
button_email.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
View view1 = getLayoutInflater().inflate(R.layout.first, null);
button_Email_click(view1);
}
});
}
09-29 23:33:45.450: E/WindowManager(27052): Activity com.abc.abc.First has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42cc5de0 that was originally added here
09-29 23:33:45.450: E/WindowManager(27052): android.view.WindowLeaked: Activity com.abc.abc.First has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42cc5de0 that was originally added here
09-29 23:33:45.450: E/WindowManager(27052): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:412)
09-29 23:33:45.450: E/WindowManager(27052): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:312)
09-29 23:33:45.450: E/WindowManager(27052): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
09-29 23:33:45.450: E/WindowManager(27052): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
09-29 23:33:45.450: E/WindowManager(27052): at android.view.Window$LocalWindowManager.addView(Window.java:554)
09-29 23:33:45.450: E/WindowManager(27052): at android.app.Dialog.show(Dialog.java:277)
09-29 23:33:45.450: E/WindowManager(27052): at com.abc.abc.First.onBackPressed(First.java:323)
09-29 23:33:45.450: E/WindowManager(27052): at android.app.Activity.onKeyUp(Activity.java:2192)
09-29 23:33:45.450: E/WindowManager(27052): at android.view.KeyEvent.dispatch(KeyEvent.java:2729)
09-29 23:33:45.450: E/WindowManager(27052): at android.app.Activity.dispatchKeyEvent(Activity.java:2431)
09-29 23:33:45.450: E/WindowManager(27052): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2009)
09-29 23:33:45.450: E/WindowManager(27052): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3942)
09-29 23:33:45.450: E/WindowManager(27052): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3890)
09-29 23:33:45.450: E/WindowManager(27052): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3019)
09-29 23:33:45.450: E/WindowManager(27052): at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 23:33:45.450: E/WindowManager(27052): at android.os.Looper.loop(Looper.java:137)
09-29 23:33:45.450: E/WindowManager(27052): at android.app.ActivityThread.main(ActivityThread.java:4898)
09-29 23:33:45.450: E/WindowManager(27052): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 23:33:45.450: E/WindowManager(27052): at java.lang.reflect.Method.invoke(Method.java:511)
09-29 23:33:45.450: E/WindowManager(27052): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
09-29 23:33:45.450: E/WindowManager(27052): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
09-29 23:33:45.450: E/WindowManager(27052): at dalvik.system.NativeStart.main(Native Method)
我尝试写为dialog1.create().show();
但是它会以.create()
的红色加下划线并将其描述为The method create() is undefined for the type Dialog
。
我发现红色logcat只会在按下退出按钮时出现...是不是因为它会显示一个对话框,但活动已关闭?我已经宣布了dialog1 ......如何修改它?谢谢!
答案 0 :(得分:1)
也可以在这两种方法中关闭对话框,例如
alert_quit.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
First.this.finish();
dialog1.dismiss();
}
});
button_email.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
View view1 = getLayoutInflater().inflate(R.layout.first, null);
button_Email_click(view1);
dialog1.dismiss();
}
});
答案 1 :(得分:0)
我认为您应该将View.
添加到onClickListeners
和Override
。
离。
alert_cancel.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dialog1.dismiss();
return;
}
});
答案 2 :(得分:0)
在onKeyDown中尝试相同而不是onBackPressed。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Your code
}
return super.onKeyDown(keyCode, event);
}
答案 3 :(得分:0)
您也可以尝试dialog1.cancel()
。基本上问题可能是因为对话框没有被取消或被解雇,它就会泄漏。