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

查看:192
本文介绍了java android getResources()。getIdentifier()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从r获取id的int id值?当我使用 getIdentifier 时,它只返回 0

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());

修改

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;


推荐答案

问题似乎是您正在转换<$小写的c $ c> drawerSelection 。如R.java文件中所示,标识符的大小写被保留。尝试调用:

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());

这篇关于java android getResources()。getIdentifier()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆