我想在mainactivity中单击后退按钮时添加退出警告。为此我使用此代码
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
final Dialog dialoge=new Dialog(MainActivity.this,android.R.style.Theme_Translucent_NoTitleBar);
dialoge.setContentView(R.layout.popup_layout);
Button yes=(Button)dialoge.findViewById(R.id.yes);
yes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity.this.finish();
}
});
Button no=(Button)dialoge.findViewById(R.id.no);
no.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialoge.cancel();
}
});
dialoge.show();
}
这是有效的,但在退出时会显示一些错误。
Logcat是
03-21 10:25:57.192: E/WindowManager(14488): Activity com.example.design.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@447fec28 that was originally added here
03-21 10:25:57.192: E/WindowManager(14488): android.view.WindowLeaked: Activity com.example.design.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@447fec28 that was originally added here
03-21 10:25:57.192: E/WindowManager(14488): at android.view.ViewRoot.<init>(ViewRoot.java:251)
03-21 10:25:57.192: E/WindowManager(14488): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
03-21 10:25:57.192: E/WindowManager(14488): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-21 10:25:57.192: E/WindowManager(14488): at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-21 10:25:57.192: E/WindowManager(14488): at android.app.Dialog.show(Dialog.java:241)
03-21 10:25:57.192: E/WindowManager(14488): at com.example.design.MainActivity.onBackPressed(MainActivity.java:97)
03-21 10:25:57.192: E/WindowManager(14488): at android.app.Activity.onKeyUp(Activity.java:1895)
03-21 10:25:57.192: E/WindowManager(14488): at android.view.KeyEvent.dispatch(KeyEvent.java:1283)
03-21 10:25:57.192: E/WindowManager(14488): at android.app.Activity.dispatchKeyEvent(Activity.java:2075)
03-21 10:25:57.192: E/WindowManager(14488): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1673)
03-21 10:25:57.192: E/WindowManager(14488): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2493)
03-21 10:25:57.192: E/WindowManager(14488): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2463)
03-21 10:25:57.192: E/WindowManager(14488): at android.view.ViewRoot.handleMessage(ViewRoot.java:1752)
03-21 10:25:57.192: E/WindowManager(14488): at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 10:25:57.192: E/WindowManager(14488): at android.os.Looper.loop(Looper.java:143)
03-21 10:25:57.192: E/WindowManager(14488): at android.app.ActivityThread.main(ActivityThread.java:4914)
03-21 10:25:57.192: E/WindowManager(14488): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 10:25:57.192: E/WindowManager(14488): at java.lang.reflect.Method.invoke(Method.java:521)
03-21 10:25:57.192: E/WindowManager(14488): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-21 10:25:57.192: E/WindowManager(14488): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-21 10:25:57.192: E/WindowManager(14488): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:5)
你不会忽视那个警告泄漏的对话框。
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// here you need to dismiss your dialog before finishing activity
if (dialoge != null) {
dialoge.cancel();
dialoge = null;
}
MainActivity.this.finish();
}
答案 1 :(得分:2)
你可以试一试,只需按下这样的按钮:
Button yes=(Button)dialoge.findViewById(R.id.yes);
yes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialoge.cancel();
MainActivity.this.finish();
}
});
在退出应用程序之前需要取消对话框。这就是为什么在对话框打开时显示leaked window error
的原因。
答案 2 :(得分:1)
也解除对话,在完成活动后致电dailog.dismiss()
或dialog.cancel()
,我测试了您的代码,它应如下所示:
@Override
public void onBackPressed() {
final Dialog dialoge=new Dialog(SecondActivity.this,android.R.style.Theme_Translucent_NoTitleBar);
dialoge.setContentView(R.layout.popup_layout);
Button yes=(Button)dialoge.findViewById(R.id.yes);
yes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
dialoge.cancel();
}
});
Button no=(Button)dialoge.findViewById(R.id.no);
no.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
dialoge.dismiss();
}
});
dialoge.show();
}
答案 3 :(得分:1)
dialoge.dismiss();
在yes按钮
的onClick方法中实现