简体   繁体   English

我的Android应用无法识别R.string资源-可能是什么原因?

[英]My android app does not recognize R.string resources - what could be the reason?

I learn android with sample apps from textbook. 我通过课本中的示例应用程序学习了android。 In the given sample source R.string.something is recognized: 在给定的示例源R.string.something中可以识别: 通用1

However, in my own EXACT source code, same R.strings are not recognized- highlighted as errors: 但是,在我自己的EXACT源代码中,相同的R.strings无法识别-突出显示为错误:

通用2

Does anyone know how to fix it? 有谁知道如何修理它?

尝试清理您的项目,如果什么都不做,请单击问题选项卡,查看是否有任何与构建相关的问题,您可能缺少必需的jar或其他东西!

These are following reason possible. 这些是可能的以下原因。

1-: You import android.R; 1-:您导入android.R;

2-: Any error in xml files. 2:xml文件中的任何错误。

3-: Please check you String.xml file may be any error or declare a string more then two time. 3-:请检查您的String.xml文件是否有错误或两次以上声明一个字符串。

I've had this many time with eclipse, and it was not a setup / code problem on my side. 我在Eclipse上度过了很多次,这对我而言不是设置/代码问题。 Generally, cleaning, rebuilding and sometime even stopping and restarting eclipse solved it. 通常,清洁,重建甚至有时停止并重新启动Eclipse都可以解决该问题。 And yes, it's a pain in the neck ... 是的,这是脖子上的疼痛...

I assume you are new to Android. 我认为您是Android的新手。

If so, there are two ways of using text strings in Buttons, textviews and so on: 如果是这样,则有两种在Buttons,textviews等中使用文本字符串的方式:

1) Hardcoded string - you put the text you want in quotation marks (""), for example: 1)硬编码的字符串-您将所需的文本放在引号(“”)中,例如:

yourTextview.setText("Hardcoded string");

2) You can call the text from your String resources (res/values/strings.xml). 2)您可以从String资源(res / values / strings.xml)调用文本。 That is a much better approach seeing is is easier to translate, make changes and so on. 这是一个更好的方法,因为它看起来更容易翻译,进行更改等。

In your strings.xml file you can create all your string values, and call them from there. 在您的strings.xml文件中,您可以创建所有字符串值,然后从那里调用它们。 Like in you example, if in your strings.xml file you have for example: 像您的示例一样,例如,如果在strings.xml文件中具有:

<string name="delete">This is String resource</string>

you can then call the string from there, like so : 然后您可以从那里调用字符串,如下所示:

yourTextview.setText(R.string.delete);

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM