我試圖從res/raw加載文本文件。我查看了幾個代碼片段,嘗試了幾種方法,但似乎沒有一個適合我。我目前正在努力的代碼是這樣的Android從Res錯誤加載文件
TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
helloTxt.setText(readTxt());
}
private String readTxt() {
InputStream inputStream = getResources().openRawResource(R.raw.hello);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1) {
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
但它遭受與所有其他人一樣的問題。
a)(TextView)findViewById(R.id.hellotxt);
表示折舊和Eclipses建議遷移代碼。
b)getResources()
不被識別,只是建議我添加一個名爲getResources()的方法。
最初我想使用資產文件夾,但得到了與b)相同的錯誤,但與getAssets()。
這是一個單獨的類文件,我實現這個叫public class PassGen{}
與所謂public String returnPass(){}
上面的代碼工作在Android 2.3.3。 –
如果在Activity類中,但不在單獨的附加類中,則會發生 – jskrwyk
這是什麼意思?ad-on class –