我如何从R中获取一个ID的int值?当我使用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>
日志
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
的值是多少?您确认已经定义了该名称的id资源吗? - Ted HoppdrawerSelection
变量具体的值是什么。同时,请发布定义相应id为资源的适用xml代码。(顺便说一下,在调用toLowerCase
时可以省略默认语言环境参数,因为它会自动使用默认值,这样只会使你的代码变得混乱。) - Ted Hopp