如何從R獲取int id值來獲取id?當我使用getIdentifier
時,它僅返回0
。java android getResources()。getIdentifier()
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;
int id = getResources().getIdentifier("resourcename", "drawable", getPackageName());
getResources
回報int
:
當它返回0時,表示沒有找到id。 'drawerSelection'的價值是什麼?你確認你有一個用這個名字定義的id資源嗎? –
您需要發佈一些關於該問題的更多信息以獲得更好的回覆。使用調試器(或一個日誌語句)來確定'drawerSelection'具體的值。將適用的XML定義爲資源。 (順便說一句,你可以在'toLowerCase'的調用中省略默認的區域設置參數;這就是它會使用它的地方,它只是混亂了你的代碼。) –