2013-06-28 29 views
1

最初我的數組看起來像這樣來解決:陣列不能sports_array

<string-array name="sports_array"> 
    <item>Shuttle Badminton</item> 
    <item>Tennis</item> 
    <item>Table Tennis</item> 
    <item>Basket Ball</item> 
    <item>Foot Ball</item> 
    <item>Volley Ball</item> 
    <item>Hockey</item> 
    <item>Swimming</item> 
</string-array> 

它位於string.xml但是當我試圖用這個代碼來檢索它,它不會顯示該數組。我得到的錯誤是數組無法解析或不是字段。

String[] sports = getResources().getStringArray(R.array.sports_array); 

我打掃我的項目兩次,錯誤仍然存​​在,但我不知道那是什麼,我把在string.xml陣列消失。所以我在這裏讀了幾個問題,其中一個選項是創建一個array.xml並將我的陣列放置在那裏而不是在string.xml中。我仍然得到同樣的錯誤。我究竟做錯了什麼?

+0

只是檢查沒有你不小心移動的 「值」文件夾之外的「res」文件夾是這樣的:http://stackoverflow.com/questions/9252098/android-array-cannot-be-resolved-or-is-not-a-field? –

+1

由於資源未正確加載,'R' java類缺少'array'類。確保你的資源文件中沒有隱藏的XML錯誤。 – Voicu

+0

你的課是否擴展活動? – Raghunandan

回答

1

您可以試試這個解決方案。

創建fetcharr.xmlRES /價值/ fetcharr.xml

<?xml version="1.0" encoding="utf-8"?> 
    <resources> 
     <string-array name="sports_array"> 
      <item>Tennis</item> 
      <item>Table Tennis</item> 
      <item>Basket Ball> 
     </string-array> 
    </resources> 

now in activity class 
Resources res = getResources(); 
String[] planets = res.getStringArray(R.array.sports_array); 
1

確保您在strings.xml中定義的: <string name="action_settings">Action Settings</string>