java android getResources().getIdentifier()[英] java android getResources().getIdentifier()

本文是小编为大家收集整理的关于java android getResources().getIdentifier()的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何从R中获取INT ID值?当我使用getIdentifier时,它只是返回0.

 int i = getArguments().getInt(SELECTION_NUMBER);
 String drawerSelection = getResources().getStringArray(R.array.drawerSelection_array)[i];

int panelId = this.getResources().getIdentifier(drawerSelection.toLowerCase(),"id",getActivity().getPackageName());

编辑

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/Bus_Schedules"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

log

06-10 21:24:30.372: I/System.out(3572): Selection = Bus_Schedules
06-10 21:24:30.372: I/System.out(3572): panelId = 0

r.java

    public static final class id {
    public static final int Bus_Schedules=0x7f090004;
    public static final int basemenu=0x7f090005;
    public static final int content_frame=0x7f090001;
    public static final int drawer_layout=0x7f090000;
    public static final int left_drawer=0x7f090002;

推荐答案

问题似乎是您将drawerSelection转换为较低的情况.正如R.Java文件中明确的,保留了标识符的情况.尝试致电:

int panelId = this.getResources().getIdentifier(drawerSelection,"id",getActivity().getPackageName());

其他推荐答案

刚刚在我现在写的项目中退房:

int id = getResources().getIdentifier("resourcename", "drawable", getPackageName());

getResources返回int.

更新:检查R.array.drawerSelection_array仅包括现有元素的ID.

其他推荐答案

尝试此方法.

public static int getResId(String fieldName, Context context, Class<?> c) {

    try {
        Field field= c.getDeclaredField(fieldName);
        return field.getInt(field);
    } catch (Exception e) {
        return 0;
    } 
}

呼叫

 //edtUserName is my field id
 getResId("edtUserName", context, id.class);

本文地址:https://www.itbaoku.cn/post/978795.html

问题描述

How do I get the int id value from R. for an id? When I use getIdentifier its just returns 0.

 int i = getArguments().getInt(SELECTION_NUMBER);
 String drawerSelection = getResources().getStringArray(R.array.drawerSelection_array)[i];

int panelId = this.getResources().getIdentifier(drawerSelection.toLowerCase(),"id",getActivity().getPackageName());

Edit

Xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/Bus_Schedules"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

log

06-10 21:24:30.372: I/System.out(3572): Selection = Bus_Schedules
06-10 21:24:30.372: I/System.out(3572): panelId = 0

R.java

    public static final class id {
    public static final int Bus_Schedules=0x7f090004;
    public static final int basemenu=0x7f090005;
    public static final int content_frame=0x7f090001;
    public static final int drawer_layout=0x7f090000;
    public static final int left_drawer=0x7f090002;

推荐答案

The problem appears to be that you are converting drawerSelection to lower case. As is clear in the R.java file, the case of the identifier is preserved. Try calling:

int panelId = this.getResources().getIdentifier(drawerSelection,"id",getActivity().getPackageName());

其他推荐答案

Just checked back in a project I'm writing right now:

int id = getResources().getIdentifier("resourcename", "drawable", getPackageName());

getResources returns int.

UPDATE: Check R.array.drawerSelection_array to include only ID's of existing elements.

其他推荐答案

Try this method.

public static int getResId(String fieldName, Context context, Class<?> c) {

    try {
        Field field= c.getDeclaredField(fieldName);
        return field.getInt(field);
    } catch (Exception e) {
        return 0;
    } 
}

Calling

 //edtUserName is my field id
 getResId("edtUserName", context, id.class);