2013-05-13 50 views
0

我有一個應用程序使用存儲在SD卡上的數據,但我的問題是,我想在應用程序終止之前顯示一個帶有錯誤消息的對話框。我試圖創建一個不在活動中但是在一個簡單類中的對話框。我使用的代碼是未知的。對於第一部分和第二部分來說,每件事都是可以的。當SD卡安裝時,當我使用nexus谷歌平板電腦。我想告訴他們在應用程序崩潰之前他們沒有SDCard,或者啓動另一個用於告訴用戶應用程序需要一個Sdcard的Activity。我使用的代碼如下。當他們沒有SD卡時,我的應用程序打印日誌崩潰。如何使用Alert Box?

public File getRootDirectory() 


{ 
     if (this.rootDirectory == null) 
     { 
      File sdCardRoot = MainApplication.getInstance().getSDCardRootDirectory(); 
      if (sdCardRoot != null) 
      { 
       this.rootDirectory = sdCardRoot; 
      } 
      else if(Build.BRAND.equals("google")) 
      { 
       this.rootDirectory = Environment.getExternalStoragePublicDirectory(MainApplication.Appli_DIRECTORY); 
      } else { 
       Log.d(CLASS_TAG, " their is no carte Sd "); 


       /** 
       * I am trying to start a Fail Activity 
       */ 
       //Intent intent = new Intent(context, FailActivity.class) 
       //.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       //context.startActivity(intent); 

      } 
      Log.i(CLASS_TAG, "Root directory set to :" + this.rootDirectory.getAbsolutePath()); 
     } 
     return this.rootDirectory; 
    } 

回答

-1

可能您必須在UI線程中創建alertbox。

0

試試這個

boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 

    if(!isSDPresent) 
    { 
     // yes SD-card is present 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
      builder.setMessage("Sorry,SD Card not Found") 
        .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
         finish(); 
         } 
        }); 
      builder.setCancelable(false); 
        // Create the AlertDialog object and return it 
      builder.create(); 
      builder.show(); 


    }