0

我在访问 LinearLayout 内的相对布局内的 TextView 时遇到这个问题

假设我在文件 row.xml 中有这些视图

<LinearLayout>
    <TextView android:id="@+id/title" />
    <RelativeLayout android:id="@+id/a_parent">
        <TextView android:id="@+id/a">
    </RelativeLayout>
    <RelativeLayout android:id="@+id/b_parent">
        <TextView android:id="@+id/b">
    </RelativeLayout>
</LinearLayout>

现在我想从一个活动中更改 TextViews 的值

LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout temp=(LinearLayout) li.inflate(R.layout.row, null);
((TextView)temp.findViewById(R.id.title)).setText("This is title");
RelativeLayout rl=(RelativeLayout)temp.findViewById(R.id.a_parent);
((TextView)rl.findViewById(R.id.a)).setText("an this is the content of a");

我可以成功设置id为'title'的TextView,但是在接近最后一行时出现错误。它说错误是由 android.content.res.Resources$NotFoundException: String resource ID #0x1 引起的

谁能告诉我出了什么问题,如何修复它,以及为什么代码不能按我预期的那样工作?

谢谢

4

3 回答 3

3

您的异常是字符串的 ResourcesNotFoundException。您是否使用设置 TextView 的值getString?如果是这样,您确定该字符串存在于您的 strings.xml 文件中吗?如果没有,您是否可能会使用R.string.name而不是引用您的一种观点R.id.name?看起来您修改了要发布的代码,如果您准确地发布了您在原始代码中所做的事情以及完整的堆栈跟踪,可能会更容易提供帮助。

有时会出现资源问题,因为 R 文件没有与新代码保持同步。我也会尝试重建/清理你的项目,看看是否有帮助。

于 2012-09-21T17:03:47.760 回答
1

当您尝试在 Textview 中设置整数值时,通常会出现此错误,如下所示:

   textview.setText(1);

在这种情况下,android 将认为整数值作为字符串资源 id 并检查字符串 strings.xml 并抛出 Resources$NotFoundException。请检查您的代码。如果没有发布您必要的代码和 logcat 输出。

于 2012-09-21T17:15:19.577 回答
0
   LinearLayout temp=(LinearLayout) li.inflate(R.layout.row....

在您的 XML 文件中,LinearLayout 没有声明“android:id”属性。

于 2012-09-21T16:59:55.757 回答