1

最近,我遵循了有关如何创建警报对话框的教程,当我单击删除按钮时,它应该在删除之前向我显示警报,但它却崩溃了。有人能告诉我我的错误在哪里吗?

下面是我的代码:

// Delete button click event
    btnDeleteEvent.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // alerting user before deleting event
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    getApplicationContext());
            // set title
            alertDialogBuilder.setTitle("Warning!");
            // set dialog message
            alertDialogBuilder
                    .setMessage("Are you sure to delete event?")
                    .setCancelable(false)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    // if yes, deleting event in background
                                    // thread
                                    new DeleteEvent().execute();
                                }
                            })
                    .setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    // if this button is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }
    });

这是我的日志:

08-18 02:02:45.804: E/AndroidRuntime(1288): FATAL EXCEPTION: main
08-18 02:02:45.804: E/AndroidRuntime(1288): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-18 02:02:45.804: E/AndroidRuntime(1288):     at android.view.ViewRoot.setView(ViewRoot.java:509)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at android.app.Dialog.show(Dialog.java:241)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at com.stts.sparetimetradingsystem.employer.EditEventActivity$5.onClick(EditEventActivity.java:270)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at android.view.View.performClick(View.java:2408)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at android.view.View$PerformClick.run(View.java:8816)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at android.os.Handler.handleCallback(Handler.java:587)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at android.os.Looper.loop(Looper.java:123)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at java.lang.reflect.Method.invokeNative(Native Method)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at java.lang.reflect.Method.invoke(Method.java:521)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-18 02:02:45.804: E/AndroidRuntime(1288):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

6

尝试使用 Activity 的上下文而不是应用程序上下文

于 2012-08-17T18:10:50.783 回答
0

使用 dialog.dismiss() 甚至将 getApplication 上下文更改为 YourActivity.this

可以参考http://codinglookseasy.blogspot.in/2012/07/alert-box-code.html

于 2012-08-17T18:11:26.073 回答
0

根据这个相关问题,您应该在构造函数中使用this而不是。getApplicationContext()

public void onClick(View arg0) {
    // alerting user before deleting event
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
于 2012-08-17T18:15:30.830 回答