在创建AlertDialog

时间:2015-10-22 15:10:23

标签: java android android-studio android-alertdialog

我有一个有一些课程的项目。

扩展Activity的一个类有一个按钮。 这是该类的onCreate方法。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.select_car);
/////// some codes and behaviors here 

直到我到达一个按钮的方法 单击该按钮时,会将一些数据插入到我的数据库中, 但在此之前,我想得到最终用户的确认,他所选择的内容是否得到纠正 首先,我想设置一个肯定的答案,这就是我在那个特定的Button中所做的。

            choose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//i want to get Confirm from end user, so i did this
                final AlertDialog.Builder builder = new AlertDialog.Builder(getApplication());
                builder.setMessage("Are You Sure To do this?")
                        .setTitle("Alert");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
AlertDialog dialog = builder.create();

// some data Insert into my DB 
    }
            });

当我运行我的项目并实现此类时,我在Android设备中遇到此错误:

  

不幸的是“MyApplicationName”已停止。

这个来自android-studio的日志

10-22 18:36:17.910  14478-14478/com.theonlyone.behtakapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.theonlyone.behtakapp, PID: 14478
    android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:660)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:286)
            at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
            at com.theonlyone.behtakapp.Car$5.onClick(Car.java:306)
            at android.view.View.performClick(View.java:4470)
            at android.view.View$PerformClick.run(View.java:18796)
            at android.os.Handler.handleCallback(Handler.java:808)
            at android.os.Handler.dispatchMessage(Handler.java:103)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)

我该怎么办?

2 个答案:

答案 0 :(得分:0)

  

$ BadTokenException:无法添加窗口 - 令牌null不适用于   应用

因为在getApplication()中使用AlertDialog.Builder传递应用程序上下文。

显示当前活动的提醒,该活动在Car.this

中正在运行v.getContext()AlertDialog.Builder

答案 1 :(得分:0)

应该是这样的

   choose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//i want to get Confirm from end user, so i did this
                    final AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                    builder.setMessage("Are You Sure To do this?")
                           .setTitle("Alert");
                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // some data Insert into my DB 
                        }
                    });
                    builder.show();
                }
        });