gpt4 book ai didi

java - 对话框导致应用程序崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 08:54:00 26 4
gpt4 key购买 nike

这就是问题所在,我有一个对话框只在用户第一次安装我的应用程序时出现。在使用我自己的设备进行测试后,我发现在框弹出后什么都不做会导致应用程序崩溃。我的 Intent 是让这个对话框成为用户在安装应用程序后看到的第一件事。它确实会出现,但如果用户在大约 3 秒内没有按下按钮,则会导致应用程序崩溃。但是,如果我在崩溃后重新启动该应用程序,那么该框将永远显示,直到用户按下一个按钮,这是我为该框设定的目标。

这是日志:

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

最佳答案

该错误意味着您试图在退出 Activity 后显示对话框。需要在activity的onPause()中调用dialog上的dismiss()方法解决问题

关于java - 对话框导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20575452/

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