我尝试从txt文件中随机读取行,然后在toast中显示此行,
我把文件放在assets文件夹中并编写了这段代码:
BufferedReader reader = new BufferedReader(new FileReader("assets/tips.txt"));
String line = reader.readLine();
List<String> lines = new ArrayList<String>();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
Random r = new Random();
String randomLine = lines.get(r.nextInt(lines.size()));
Toast toast = Toast.makeText(this, randomLine, 30000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
我犯的错误是什么,所以我的应用程序没有显示任何事情并崩溃?