这是问题,我有一个对话框仅在用户首次安装我的应用程序时出现。在使用我自己的设备进行测试后,我看到在弹出框后不进行任何操作都会导致应用崩溃。我的意图是让此对话框成为用户在安装应用程序后首先看到的东西。它的确出现了,但是如果用户在大约3秒钟内未按任何按钮,则会导致应用程序崩溃。但是,如果我在崩溃后重新启动该应用程序,则该框将永久显示,直到用户按下按钮为止,这是我为该框指定的目标。
这是logcat:
12-13 15:25:28.625: ERROR/WindowManager(15315): Activity com.nick.simplequiz.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41506ff0 V.E..... R....... 0,0-684,679} that was originally added here
android.view.WindowLeaked: Activity com.nick.simplequiz.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41506ff0 V.E..... R....... 0,0-684,679} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:458)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
at android.app.Dialog.show(Dialog.java:286)
at com.nick.simplequiz.MainActivity.onCreate(MainActivity.java:216)
at android.app.Activity.performCreate(Activity.java:5165)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
at dalvik.system.NativeStart.main(Native Method)
这是根据logcat发生错误的行:
alertDialog.show();
是什么原因导致这种情况发生?
这是onCreate
方法内部的代码:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setTitle("...."); //Set the title of the box
alertDialogBuilder.setMessage("....");
alertDialogBuilder.setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); //when they click dismiss we will dismiss the box
SharedPreferences.Editor edit2 = sp.edit();
edit2.putInt("SHOW", 1);
edit2.commit();
}
});
alertDialogBuilder.setNegativeButton("", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
.......
}
});
AlertDialog alertDialog = alertDialogBuilder.create(); //create the box
alertDialog.show(); //*************error happens here
该错误表示您退出活动后正在尝试显示对话框。您需要在活动dismiss()
的对话框中调用方法onPause()
以解决问题
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句