我已經查看了噸這個問題的不同解決方案,但似乎沒有任何工作。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