2013-06-12 82 views
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的,貌似問題是什麼「令牌無效」有關,但我不知道這件事:

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) 
+0

請發佈logcat輸出以幫助我們找到您的問題。 – JorganPubshire

+0

當然,這是:) – moictab

+3

@moictab使用Activity.this而不是getApplicationContext() – TheFlash

回答

4

據對this問題的答案一個WindowManager.BadTokenException時,當你傳遞了錯誤的上下文。

因此改變使用本:

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

,而不是這樣的:

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

使用這樣

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

調用此我thod

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(); 
    } 
相關問題