2011-07-24 45 views
1

我在寫一個case語句來啓動和xml解析器活動,並且想根據用戶點擊的內容設置不同的輸入流,我可以從一個變量設置輸入流嗎?可以從一個變量設置資源嗎?

這裏的InputStream的:

InputStream inputStream = getResources().openRawResource(R.raw.myxmlfile); 

我已經嘗試設置一個字符串getResources().openRawResource(R.raw.myxmlfile)並沒有工作,我也嘗試設置XML文件作爲一個字符串,並沒有奏效。這甚至有可能嗎?

回答

3

這不是一個字符串。它應該被聲明爲整數。

int[] xmlfile = { R.raw.xml1, R.raw.xml2 }; 
getResources().openRawResource(xmlfile[0]); 
+0

這完美地工作,太感謝你了!由於我是新來的論壇,所以我不能投票回答,但這是我的問題的答案。 – Jen

1

你有沒有嘗試過的XML文件移入assets folder,然後做

InputStream inputStream = context.getAssets().open(FILE_NAME);

0

試試這個:

int r = R.raw.myxmlfile; 
InputStream inputStream = getResources().openRawResource(r); 
相關問題