2012-01-03 38 views
0

我試圖做一個很簡單的事情,顯示警報,只有1個按鈕,點擊,如果我想對話框關閉,比應用程序退出\結束()如何從AlertDialog

退出程序目前我正在使用該設備的通用警報:

應用程序意外停止。請重試

在logcat中我得到:無法暫停活動

這裏是日誌:

01-03 14:49:00.670: ERROR/AndroidRuntime(22536): Uncaught handler: thread main exiting due to uncaught exception 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536): java.lang.RuntimeException: Unable to pause activity {com.SprintTwo/com.SprintTwo.SprintTwo}: java.lang.NullPointerException 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3162) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3119) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3102) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.app.ActivityThread.access$2400(ActivityThread.java:119) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1874) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.os.Looper.loop(Looper.java:123) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at java.lang.reflect.Method.invoke(Method.java:521) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at dalvik.system.NativeStart.main(Native Method) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536): Caused by: java.lang.NullPointerException 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at com.phonegap.DroidGap.onPause(DroidGap.java:736) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at com.worklight.androidgap.WLDroidGap.onPause(WLDroidGap.java:163) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.app.Activity.performPause(Activity.java:3782) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1190) 
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3149) 

這裏是我的代碼:

buttonClickListener = new AlertDialog.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 
          finish(); 
         } 

AlertDialog.Builder dlg = new AlertDialog.Builder(context); 
    dlg.setTitle(title); 
    dlg.setMessage(message); 
    dlg.setCancelable(false); 
    dlg.setPositiveButton(buttonText, buttonClickListener); 
    dlg.create(); 
    dlg.show(); 
+0

buttonClickListener在dlg.setPositiveButton中使用它爲空,所以當你點擊肯定按鈕時,你開始NPE – Selvin 2012-01-03 13:33:48

+2

在代碼中com.phonegap.DroidGap.onPause(DroidGap.java:736)'' ?當然你沒有什麼奇怪的東西? – tidbeck 2012-01-03 13:34:56

+0

你能寫我們在736行DroidGap.java上有什麼嗎?另外我很好奇在WLDroidGap.java上的第163行。那裏肯定有一些問題。 – 2012-01-03 13:52:48

回答

6

嘗試System.exit(0)而不是finish()

+2

這不是關閉Android應用程序的正確方式,但對於我的特定情況下它的工作...謝謝 – 2012-01-03 14:55:11

-1

嘗試,如果工作

private void ConfirmAlert() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("CLOSE"); 
    builder.setMessage("Do You Want to Close the Application").setCancelable(false) 
    .setPositiveButton("Yes", 
      new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      dialog.dismiss(); 
      onYesClick(); 

     } 


    }).setNegativeButton("No", 
      new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      dialog.cancel(); 
      onNoClick(); 
     } 
    }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
} 
private void onYesClick() { 
    Intent setIntent = new Intent(Intent.ACTION_MAIN); 
    setIntent.addCategory(Intent.CATEGORY_HOME); 
    setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(setIntent); 

    ActivityName.this.finish(); 



}private void onNoClick() { 


} 
0

的問題是你不能從暫停模式完成的活動。

想法是處理它與國旗。 使用標誌檢查將退出代碼寫入Resume()方法中。在對話框中啓用標誌。

喜歡的東西(下面的代碼是假的),

boolean APP_EXIT_FLAG = false; 
Resume(){ 

if(APP_EXIT_FLAG) 
finish(); 

} 

而且你的警告對話框代碼,

buttonClickListener = new AlertDialog.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 

          APP_EXIT_FLAG = true; 
          dialog.dismiss(); 
         } 

AlertDialog.Builder dlg = new AlertDialog.Builder(context); 
    dlg.setTitle(title); 
    dlg.setMessage(message); 
    dlg.setCancelable(false); 
    dlg.setPositiveButton(buttonText, buttonClickListener); 
    dlg.create(); 
    dlg.show(); 

希望它能幫助。

+0

從哪裏可以打電話給Resume()? – 2012-01-03 14:26:21

+0

如果您的應用程序處於暫停模式,它會自動調用恢復(生命週期方法) – sohilv 2012-01-04 05:22:49