我已经查看了吨这个问题的不同解决方案,但似乎没有任何工作。Android应用程序,阅读文本文件从水库
我正在尝试制作一个读取文本文件并在屏幕上显示其部分的应用程序。尽管我正在阅读文件时遇到问题,但我不知道自己做错了什么。我一直在调试代码,它会抛出一个IO java异常(fileNotFound)..它不会执行代码的文件读取部分..我从来没有到达while循环..它在抛出异常之前抛出异常。
我把文件中的/res/raw directory
在logcat的错误输出是:threadid=3 Thread exiting due to uncaught exception
当我编译它也抱怨上线48一个NullPointerException我不,因为我不明白的代码似乎无法达到调试器中的那部分代码。
我在做什么错?
public class AndroidReadTest extends Activity {
HashMap<String, ArrayList<String>> table = new HashMap();
Random rand = new Random();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String category = "";
ArrayList<String> temp = new ArrayList();
try {
/** AssetFileDescriptor descriptor = getAssets().openFd("AndroidReadTest/res/raw/questions");
BufferedReader in = new BufferedReader(new FileReader(descriptor.getFileDescriptor())); */
BufferedReader in = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.questions))); // I have tried adding .txt
while(in.readLine()!=null){
......
}
table.put(category, temp);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(click);
}
public String get(ArrayList<String> questions){
int temp = questions.size();
String str = questions.get(rand.nextInt(temp));
return str;
}
private OnClickListener click = new OnClickListener(){
public void onClick(View V){
TextView text = (TextView) findViewById(R.id.textView1);
text.append(get(table.get("---Cathegory")));
}
};
}
使用资产可能是更好的路要走,但我认为这应该工作了。哪一行是第48行?如果它是'BufferedReader in = ...'行,那么将该行分成不同的变量(一个用于“Resources”,一个用于InputStream等),然后再次运行它,它会告诉你哪个部分正在破坏。 – 2012-01-10 16:48:01