【问题标题】:Android - getResources().getIdentifier() not finding the resourceAndroid - getResources().getIdentifier() 找不到资源
【发布时间】:2011-09-08 14:14:35
【问题描述】:

我有一个包含以下资源的 xml 文件

    <LinearLayout android:id="@+id/t214" android:padding="3dip"></LinearLayout>

    <LinearLayout android:id="@+id/t215" android:padding="3dip"></LinearLayout>

等等

我有以下代码:

String id = "t" + c.get(Calendar.DAY_OF_WEEK) + c.get(Calendar.HOUR_OF_DAY);
Log.v("calendar", "id string: " + id);
// Get handle of LinearLayout
LinearLayout ll = (LinearLayout) findViewById(CalendarViewActivity.this.getResources().getIdentifier(id, "id", getPackageName()));

根据日志,id 字符串变量的值为 't214',因此 getResources().getIdentifier() 应该返回 int 标识符,以便它可以在 findViewById 中使用,但 ll 被返回为 null。

非常感谢!

【问题讨论】:

  • 你在创建函数中使用 setcontentview 吗?
  • 是的,setContentView(R.layout.calendar_view_week);这就是包含我想要处理的视图的布局
  • LinearLayouts 嵌套的事实可能是个问题吗? (它们嵌套在 TableRow 中)
  • 说我检查了 R.java 并且所有的 LinearLayouts 都在那里
  • 我发现问题是由 id 中包含数字的 id 引起的。这是不允许的吗?

标签: java android xml view


【解决方案1】:

我不确定我是否理解了这个问题......但是,findViewById() 的一般模式建议这样的代码:

LinearLayout ll = (LinearLayout) findViewById(R.id.t214);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 2012-11-23
    • 2011-12-11
    • 1970-01-01
    相关资源
    最近更新 更多