如何在Android中使用Resource.getString(id)访问资源文件中的特定字符串

如何在Android中使用Resource.getString(id)访问资源文件中的特定字符串,android,resources,Android,Resources,我想知道我有一个string.xml资源文件,其中包含状态消息。现在我想根据它们的id来处理它们。我已经编写了以下代码,但是在Message=res.getString(Msgid)上失败了和日志未找到资源异常 有人能帮我吗 public void FileStatusMsg(int Msgid) { String Message = null; Resources res = this.getResources

我想知道我有一个string.xml资源文件,其中包含状态消息。现在我想根据它们的id来处理它们。我已经编写了以下代码,但是在
Message=res.getString(Msgid)上失败了和日志
未找到资源异常

有人能帮我吗

public void FileStatusMsg(int Msgid)


        {
                String Message = null;
                Resources res = this.getResources();


                Message = res.getString(Msgid);

                //Display Status Messages..
                AlertDialog.Builder builder = new AlertDialog.Builder(OfficeLinQ.this);
                builder.setMessage(Message).setCancelable(false).setPositiveButton(
                        "OK", new DialogInterface.OnClickListener() 
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }

假设你有一个值

<string name="msg">Message</string>

您可以使用以下方法访问它:

YourActivity.this.getResources().getString(R.string.myString);

你的msgid是什么?您是如何获得它的?您传递的Msgid是什么?Msgid的值来自哪里?看起来它实际上不是一个资源ID。是的,它不是资源ID。我现在更正了它,并在Msgid中提供了资源ID,然后它就工作了。。。。
YourActivity.this.getResources().getString(R.string.myString);