【问题标题】:String Resource is int?字符串资源是int?
【发布时间】:2014-03-16 19:15:57
【问题描述】:

Eclipse 告诉我我的字符串是一个 int?

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Uri.parse(R.string.get_question_url));
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity httpentity = new response.getEntity();
InputStream isr = httpentity.getContent();

第 2 行:HttpPost httppost = new HttpPost(Uri.parse(R.string.get_question_url)); 我想知道为什么,有人知道帮助吗?

【问题讨论】:

  • 你能告诉我 R.string.get_question_url 的价值是什么

标签: android string int


【解决方案1】:

R.string.something 返回资源的整数标识符,而不是您期望的实际字符串内容。您必须使用getString() 才能从其标识符中获取实际的字符串资源。

使用它来实际从字符串资源构建 Uri,而不是从字符串资源的标识符:

HttpPost httppost = new HttpPost(Uri.parse(getString(R.string.get_question_url)));

假设您在 Context 的子类中运行此代码。

否则,使用:

HttpPost httppost = new HttpPost(Uri.parse(yourContext.getResources().getString(R.string.get_question_url)));

【讨论】:

    【解决方案2】:

    您项目中的所有资源都由int 编号标识,无论它们是什么:Drawables、Layouts、Strings... 所以每次您将资源命名为android:id="@+id/whatever",Android 内部都将其表示为一个int,您可以通过R.resource.name 自动生成的索引访问它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多