java - 无法通过 getIdentifier 检索资源

标签 java android

我的 XML 中有以下元素

        <EditText
        android:id="@+id/kernelb11"
        android:layout_width="@dimen/matrixBoxWidthHeight"
        android:layout_height="@dimen/matrixBoxWidthHeight"
        android:layout_below="@+id/textViewb"
        android:layout_margin="@dimen/marginOne"
        android:background="@color/white1"
        android:gravity="center"
        android:inputType="numberSigned"
        android:maxLength="2"
        android:text="5" >

        <requestFocus />
    </EditText>

我正在尝试在代码中检索它的值,例如:

String name = "kernelb11";
int a = this.getResources().getIdentifier(name, null, null); // 
String as = (String) this.getResources().getText(a);

但是在 getIdentifier 处只返回“0”,谁能告诉我为什么?

最佳答案

试试这个:

int a = this.getResources().getIdentifier(name, "id", getPackageName());

正如其他人指出的那样,这不是最好的方法(尽管应该可行)。
通常,您应该使用 findViewById 来代替。

关于java - 无法通过 getIdentifier 检索资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19800156/

相关文章:

java - Maven java maven-antrun-plugin

java - 使用带有 Cookie 持久性的改造

java - 为什么 activity_main.xml 不在 Android Studio 中显示我的组件,为什么 28 API 不起作用?

android - YouTube 嵌入视频无法播放

java - android 上的原始类型与托管类型——我应该在我的应用程序中使用哪一个

android - 调试应用程序时发生 ANR 崩溃

java - 线性布局层次结构和对齐问题

java - 我如何单击以检查选中了哪些复选框以及是否按下了按钮

java - 在 web 应用程序的 jboss 服务器中进行新部署后,浏览器缓存文件未更新

android - Repository 方法在 Asynchronous Retrofit 调用中设置 LiveData 值