2011-06-02 73 views
3

我想知道我擁有包含狀態消息的string.xml資源文件。現在我想根據他們的ID來處理它們。我寫了下面的代碼,但它在Message = res.getString(Msgid);和日誌Resource not found Exception上失敗。如何在Android中使用Resource.getString(id)訪問資源文件中的特定字符串

任何人都可以幫助我嗎?

public void FileStatusMsg(int Msgid) 


     { 
       String Message = null; 
       Resources res = this.getResources(); 


       Message = res.getString(Msgid); 

       //Display Status Messages.. 
       AlertDialog.Builder builder = new AlertDialog.Builder(OfficeLinQ.this); 
       builder.setMessage(Message).setCancelable(false).setPositiveButton(
         "OK", new DialogInterface.OnClickListener() 
         { 
          public void onClick(DialogInterface dialog, int id) 
          { 
           dialog.cancel(); 
          } 
         }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 
      } 
+0

什麼是你MSGID?你如何獲得它? – 2011-06-02 07:58:34

+0

你作爲Msgid傳遞了什麼? – Geobits 2011-06-02 07:58:58

+1

Msgid的值來自哪裏?看起來它實際上並不是一個資源ID。 – fleetway76 2011-06-02 07:59:45

回答

3

假設你有一個值

<string name="msg">Message</string> 

strings.xml

要訪問此值,在您的活動給:

this.getText(R.string.msg).toString() 
1

您可以使用此方法訪問它:

YourActivity.this.getResources().getString(R.string.myString); 
相關問題