2016-07-14 30 views
2

我面臨的問題是,getString()返回的不是id的字符串,而是另一個字符串。但首先這是我的代碼:Android的getString()返回錯誤的字符串

public public class ShopFragment extends Fragment { 
    ... 
    public class ShopManager(){ 
     ... 
     private TableRow[] getRowsFromItems(){ 
     ... 
     TextView headlineName = new TextView(getContext()); 
     headlineName.setText(getString(R.string.shop_headline_name)); 
     TextView headlinePrice = new TextView(getContext()); 
     headlinePrice.setText(getString(R.string.shop_headline_price)); 
     ... 
     } 
    } 
} 

的strings.xml:

<resources> 
    ... 
    <string name="show_leaderboards">Leaderboards</string> 
    <string name="show_achievements">Achievements</string> 
    ... 
    <string name="shop_headline_name">Article</string> 
    <string name="shop_headline_price">Price</string> 
    ... 
</resources> 

的問題是,是的getString(R.string.shop_headline_name)返回 「成就」 和GetString(R.string.shop_headline_price )返回「排行榜」。我不知道如何解決這個問題,我很困惑爲什麼會發生這種情況。我究竟做錯了什麼? 謝謝您的回答

+4

清理項目(例如,Android Studio中的Build> Clean Project)並查看是否有幫助。 – CommonsWare

+0

如果您已啓用**即時運行**,則可能導致此問題。它仍然是一個破碎的東西,不要使用它! – Sufian

回答

1

要使用從資源文件夾(這裏strings.xml)你的字符串,使用此代碼:

headlineName.setText(getResources().getString(R.string.shop_headline_name)); 

這應該工作。

+0

getString(int resId)在內部實現getResources()。getString(resId)。因此,不需要getResource() – Tony

5

清潔和構建應該做的伎倆。包含所有ID的R.java沒有正確生成。清潔和構建會正確生成它。

相關問題