我試圖從另一個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