![](/img/trans.png)
[英]Android - getResources().getIdentifier() not finding the resource
[英]java android getResources().getIdentifier()
如何從R獲取id的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>
日志
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.