简体   繁体   中英

Why is my alert dialog crashing my application?

My application crashes when I try to show the dialog when the user pushes the "Eliminar" (delete) button:

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();
        }
    });

Can you give some help please? I have no idea what's wrong.

Edit: Here is the logcat, looks like the problem is something "token null" related, but I have no idea about it:

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)

According to the answers on this question a WindowManager.BadTokenException occurs, when you pass in the wrong context.

So change use this:

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

instead of this:

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

Use like this

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

Call this method

  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();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM