[英]Unknown class R.string ... when extracting string resource
I am trying to extract a string resource out of my activity but get the following error -我正在尝试从我的活动中提取字符串资源,但出现以下错误 -
I don't remmber in the past that there was anything that I should have filled before the dot of the "getString" method for extracting a string out of an activity.过去我不记得我应该在“getString”方法的点之前填充任何东西,以从活动中提取字符串。 What am I missing?
我错过了什么?
You can use:您可以使用:
String myValue = getResources().getString(R.string.mystring);
but getResources()
is a method of the Context
class.但是
getResources()
是Context
类的一个方法。
It means that you can't do this:这意味着你不能这样做:
private static String BASE_URL = getResources().getString(R.string.myurl); //it DOESN'T work!!
You can use your BuildConfig
class to set these kind of values.您可以使用
BuildConfig
类来设置这些类型的值。
Also in your build.gradle
file you can config them:同样在您的
build.gradle
文件中,您可以配置它们:
buildTypes {
release {
//..
buildConfigField("String", "BASE_URL", "....")
}
//....
}
It will populate BuildConfig.BASE_URL
.它将填充
BuildConfig.BASE_URL
。
If you want to use another constant declared in another activity make sure that is declared public and import it.如果您想使用在另一个活动中声明的另一个常量,请确保将其声明为 public 并导入它。
If you wand to use a string declared in the string resource file:如果您想使用在字符串资源文件中声明的字符串:
getResources().getString(R.string.mystring);
Try to call getResources()
like this:尝试像这样调用
getResources()
:
Java爪哇
String yourString = getResources().getString(R.string.herro_app_aoi);
kotlin科特林
val yourString = resources.getString(R.string.herro_app_aoi)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.