我试图从另一个Activity
中生成的文本文件导入文本。生成的文本文件由仅包含数字和Android生成的其他随机文本的String
ArrayList
组成。当我从文件中导入文本时,我使用BufferedReader
和readLine()
将每个新号码变为Integer
ArrayList
。我将从文本文件中删除任何非数字值,并将其他活动中生成的数字拆分为“\ n”。由于Integer.parseInt导致崩溃
是我面临的问题是,当它加载Activity
Android的崩溃。我已将事业范围缩小至Integer.parseInt()
。
我的代码如下:
ArrayList<Integer> lines = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File file = new File(getFilesDir(), "test_file.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while (br.readLine() != null) {
String text = (br.readLine()).replaceAll("[^0-9]+","").trim();
Integer number = Integer.parseInt(text);
lines.add(number);
}
} catch (IOException e) {
}
TextView tv = (TextView) findViewById(R.id.helptext);
int max = 0, min = 100;
double total = 0;
for (int i = 0; i < lines.size(); i++) {
int number = lines.get(i);
max = Math.max(max, number);
min = Math.min(min, number);
total += number;
}
tv.setText("max = " + max + " min = " + min + " total = "
+ total);
IT WORKS !!!!!!我不得不将text.isEmpty()改为text.length()== 0,因为我使用的是较旧的Android API,但它工作正常! 谢谢你这么多DACWE! – BGM
@Arjan - 对不起,我确实已经阅读了关于在评论中回复的帮助,但是却意外地忽略了“@”符号。另外,对于发球台我很抱歉,我真的没有想到会造成太多麻烦。我花了很长时间来解决这个问题,并且在达克解决这个问题时非常开心,我无法对他表示感谢。我向你保证它不会再发生。顺便说一句,我不得不说,stackoverflow是最好的,如果不是最好的,在互联网上的代码解决方案的网站之一。如果你是帮助建立的人之一,非常感谢你! – BGM
@BGM,dacwe的tee不是问题,如果你阅读[this](http://meta.stackexchange.com/questions/104959/gift-reward-offered-after-accepting-an-answer)。最后一点,''堆栈溢出在它的名字中占用一个空格;-)' nitpicking-mode>'。所有这一切说:让我们清理;我正在删除我的评论 - 欢迎来到Stack Exchange! –
Arjan