我想从原始目录(rules.txt)文件中的文件中读取文本,并将其文本显示在帮助活动中 代码中没有任何错误,但是当我在模拟器中运行时,应用程序崩溃了。 任何人都可以解决这个问题..我是android的新开发者
public class HelpActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
InputStream iFile = getResources().openRawResource(R.raw.rules);
TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
String strFile = inputStreamToString(iFile);
helpText.setText(strFile);
}
private String inputStreamToString(InputStream iFile) {
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:0)
我想你应该使用这样的东西:
public static String getDataFromResources(Context context, int resID) throws IOException {
InputStream iStream = context.getResources().openRawResource(resID);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(iStream, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
iStream.close();
}
return writer.toString();
}