R.string.value 帮助 android 通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3363245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
R.string.value Help android notification
提问by Simon
whats the deal with
这是怎么回事
CharSequence contentTitle = R.string.value;
Error cannot convert from int to CharSequence. Is there a way around this or am i missing something? i tried
错误无法从 int 转换为 CharSequence。有没有办法解决这个问题,或者我错过了什么?我试过
String s = R.string.value + "";
CharSequence contentTitle = s;
it returns integers values. Any help?
它返回整数值。有什么帮助吗?
回答by Sephy
R.string.value
is a call to the static field in the class R, which is auto generated by Eclipse and which does a kind of summary of all your resources. To retrieve the string, you need to use :
R.string.value
是对 R 类中的静态字段的调用,它由 Eclipse 自动生成,并对您的所有资源进行某种汇总。要检索字符串,您需要使用:
CharSequence contentTitle = getString(R.string.value);
If you open the R class you will see that it contains only numbers that are references to the compiled resources of your project.
如果您打开 R 类,您将看到它仅包含对项目已编译资源的引用的数字。
回答by sizreaper
To retrieve the string, you need to use getString(),
要检索字符串,您需要使用 getString(),
but getString() is a method from Context class. If you want to use this method outside your Activity class, you should get link to your context first and then call:
但是 getString() 是来自 Context 类的方法。如果你想在你的 Activity 类之外使用这个方法,你应该先链接到你的上下文,然后调用:
String s = mContext.getString(R.string.somestring)
回答by TwoByteHero
R.string.value returns the reference ID number of the resource 'value'. If you look at your R class it will appear as something like this:
R.string.value 返回资源“值”的引用 ID 号。如果您查看 R 类,它将显示为如下所示:
public static final class string {
public static final int value=0x7f040007;
}
I've been experiencing issues with referencing the getString() method. The exact error that Eclipse spits at me is:
我在引用 getString() 方法时遇到问题。Eclipse 向我吐槽的确切错误是:
The method getString(int) is undefined for the type DatabaseHelper.MainDatabaseHelper
对于类型 DatabaseHelper.MainDatabaseHelper 未定义方法 getString(int)
After reading for awhile I've figured out that you must reference your application's context to get access to the getString() method. I was trying to create a private SQLDatabase helper class in a content provider, however, that was not allowing me to reference the getString() method. My solution so far is to do something like this:
阅读了一段时间后,我发现您必须引用应用程序的上下文才能访问 getString() 方法。我试图在内容提供程序中创建一个私有的 SQLDatabase 帮助程序类,但是,这不允许我引用 getString() 方法。到目前为止,我的解决方案是做这样的事情:
private class MainDatabaseHelper extends SQLiteOpenHelper {
MainDatabaseHelper(Context context) {
super(context, context.getString(R.string.createRoutesTable), null, 1);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL((getContext()).getString(R.string.createRoutesTable));
}
}
Notice these two context references:
请注意这两个上下文引用:
context.getString()
(getContext()).getString()
上下文.getString()
(getContext()).getString()
I don't know if this is the optimal long-term solution but it seems to work for the moment. Hope this helps.
我不知道这是否是最佳的长期解决方案,但目前似乎有效。希望这可以帮助。
回答by viki
You could use String s = getResources().getString(R.string.value);
also.
你也可以用String s = getResources().getString(R.string.value);
。