我想在Android Studio中讀取一個文件,並將每個字符串放入一個ArrayList中,但是當我嘗試從ArrayList中獲取該字符串時,應用程序崩潰(消息:「不幸的,應用程序已停止「)任何人都可以告訴我什麼是錯的?arraylist.get()在Android Studio中崩潰我的應用程序
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
////////////////////////////////////////////////////////////////////////////
String text = "";
tv_view = (TextView) findViewById(R.id.textview1);
Scanner s = new Scanner(System.in);
File n = new File("C:\\Users\\Admin\\AndroidStudioProjects\\LOTOS.1\\app\\src\\main\\assets\\nouns.txt");
//Instantiate Scanner s with f variable within parameters
//surround with try and catch to see whether the file was read or not
try {
s = new Scanner(n);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Instantiate a new ArrayList of String type
ArrayList<String> theWord = new ArrayList<String>();
//while it has next ..
while(s.hasNext()){
//Initialise str with word read
String str=s.next();
//add to ArrayList
theWord.add(str);
}
text = theWord.get(150);
tv_view.setText(text);
//return ArrayList
}
它在Eclipse中運行良好 –
@ShaishavJogani問題是它在事件日誌中編譯得很好,沒有錯誤。然而,該應用程序崩潰 –
如果它崩潰,肯定是一個錯誤。 –