0

当我尝试在用户按下“Eliminar”(删除)按钮时显示对话框时,我的应用程序崩溃:

botonEliminar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    getApplicationContext());
            builder.setMessage("¿Seguro que quieres eliminar?")
                    .setCancelable(false)
                    .setNegativeButton("Eliminar",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    SQLiteDatabase database = gestor.getWritableDatabase();
                                    if (database != null) {
                                        database.execSQL("DELETE FROM Organizaciones WHERE nombre_org = '"
                                                + name + "'");
                                        database.close();
                                    }

                                    Intent intent = new Intent(
                                            Consultar_organizacion.this,
                                            MainActivity.class);
                                    Consultar_organizacion.this
                                            .startActivity(intent);
                                }
                            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });

你能帮忙吗?我不知道出了什么问题。

编辑:这是logcat,看起来问题与“token null”相关,但我不知道:

06-12 15:16:59.776: W/dalvikvm(2343): threadid=1: thread exiting with uncaught exception (group=0x409fa1f8)
06-12 15:16:59.776: E/AndroidRuntime(2343): FATAL EXCEPTION: main
06-12 15:16:59.776: E/AndroidRuntime(2343): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:522)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.app.Dialog.show(Dialog.java:285)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at com.example.project.Consultar_organizacion$1.onClick(Consultar_organizacion.java:87)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.view.View.performClick(View.java:3511)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.view.View$PerformClick.run(View.java:14109)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.os.Handler.handleCallback(Handler.java:605)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.os.Looper.loop(Looper.java:137)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at java.lang.reflect.Method.invokeNative(Native Method)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at java.lang.reflect.Method.invoke(Method.java:511)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-12 15:16:59.776: E/AndroidRuntime(2343):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

4

根据这个问题的答案WindowManager.BadTokenException,当您传入错误的上下文时,就会发生这种情况。

所以改变使用这个:

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);

而不是这个:

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
于 2013-06-12T13:23:45.597 回答
0

像这样使用

botonEliminar.setOnClickListener(new View.OnClickListener() 
  {
      @Override
      public void onClick(View v) 
      {
          alert_dialog__();
      }
  });

调用这个方法

  protected void alert_dialog__() 
  {
     AlertDialog.Builder builder = new AlertDialog.Builder(
                this);
        builder.setMessage("¿Seguro que quieres eliminar?")
                .setCancelable(false)
                .setNegativeButton("Eliminar",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                SQLiteDatabase database = gestor.getWritableDatabase();
                                if (database != null) {
                                    database.execSQL("DELETE FROM Organizaciones WHERE nombre_org = '"
                                            + name + "'");
                                    database.close();
                                }

                                Intent intent = new Intent(
                                        Consultar_organizacion.this,
                                        MainActivity.class);
                                Consultar_organizacion.this
                                        .startActivity(intent);
                            }
                        });
        AlertDialog alert = builder.create();
        alert.show();
  }
于 2013-06-12T14:39:43.317 回答