2014-10-12 26 views
-1

任何人都可以幫助解釋爲什麼警告對話框不會顯示這段代碼?它位於主活動的onCreate(Bundle savedInstanceState)方法中。Android AlertDialog在應用程序退出前不顯示

//setup the export directory 
    if(!Globals.setupExportDir(s_SessionName)) 
    { 
     AlertDialog.Builder dlgbuilder = new AlertDialog.Builder(this); 
     dlgbuilder.setTitle("Error message"); 
     dlgbuilder.setMessage("Could not create the directories"); 
     dlgbuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
     { 
      public void onClick(DialogInterface dialog, int whichButton) 
      { 
       //just exit 
      } 
     }); 

     AlertDialog dlg = dlgbuilder.create(); 
     dlg.show(); 

     System.exit(0); 
    } 

我懷疑這是因爲系統在顯示對話框之前退出,但那麼做什麼纔是正確的方法呢?

在此先感謝您的任何見解。

安德烈

回答

1

爲了解決這個問題,將System.exit(0);onClick方法,使應用程序只是退出用戶在對話框中讀取消息並按下Ok後。或者甚至更好,您可以撥打ActivityName.this.finish()onClick方法關閉活動(如果這是堆棧中唯一的一個)。

+0

沒有太大的區別,但它值得一試。該應用的表現看起來好像Alertdialog不是模態。我會盡量找到解決辦法。非常感謝。 – WestWind 2014-10-12 11:35:06

+0

使用Toast來代替這個技巧,但爲什麼AlertDialog失敗仍然是一個謎。 – WestWind 2014-10-12 11:44:46

相關問題