2016-10-25 25 views
1

我不斷收到ArrayIndexOutOfBoundsException,但我不確定哪部分代碼確實會導致此錯誤。任何幫助將不勝感激。該代碼是無法在行讀取器中啓動活動,ArrayIndexOutOfBounds

 while ((line = reader.readLine()) != null) { 

      String[] wordOnLine = line.split(","); 
      geo.put(wordOnLine[0] , new GeoLocation(wordOnLine[0], Double.parseDouble(wordOnLine[1]), Double.parseDouble(wordOnLine[2]),TimeZone.getTimeZone(wordOnLine[3]))); 

     } 

該錯誤是

無法啓動活動 ComponentInfo {swindroid.suntime/swindroid.suntime.ui.Main}: java.lang.ArrayIndexOutOfBoundsException:長度= 1;索引= 1

+0

您是通過劈裂線 - ''。那麼你正在接受wordOnLine--你確定每條線都以昏迷結束嗎?另外檢查這一個:http://stackoverflow.com/questions/454908/split-java-string-by-new-line – X3Btel

+0

謝謝,爲您的答覆。是的,每一行都需要有一個camma,因爲我必須從文件中讀取以下文本。格倫莫公園,-33.79068,150.6693,澳大利亞/悉尼 – nadia

+0

@nadia我認爲你需要有2個循環,1.對於每行按\ n分割和循環直到用line.length來分割你的文本每行',' – Sodiq

回答

0

公共列表getListOfLocations(){

// String fileName = "au_locations.txt"; 
// BufferedReader reader = null; 
// StringBuilder builder = new StringBuilder(); 

    InputStream input = getResources().openRawResource(R.raw.au_locations); 
    BufferedReader reader = new BufferedReader(new InputStreamReader((input))); 
    String line; 
    StringBuilder builder = new StringBuilder(); 
    File file = new File(getFilesDir(), "au_locations.txt"); 

    try { 
    // reader = new BufferedReader(new InputStreamReader(getAssets().open(fileName))); 
    // String line; 

     while ((line = reader.readLine()) != null) { 
     // builder.append(line).append("\n"); 
      String[] wordOnLine = line.split(","); 
      geo.put(wordOnLine[0] , new GeoLocation(wordOnLine[0], Double.parseDouble(wordOnLine[1]), Double.parseDouble(wordOnLine[2]),TimeZone.getTimeZone(wordOnLine[3]))); 
      //locations.add(line); 
     } 

     if(file.exists()){ 
      FileInputStream inputFile = openFileInput("au_locations.txt"); 
      input = inputFile; 
      reader = new BufferedReader((new InputStreamReader((input)))); 
      while ((line = reader.readLine()) != null) { 
       //  builder.append(line).append("\n"); 
       String[] wordOnLine = line.split(","); 
       geo.put(wordOnLine[0] , new GeoLocation(wordOnLine[0], Double.parseDouble(wordOnLine[1]), Double.parseDouble(wordOnLine[2]),TimeZone.getTimeZone(wordOnLine[3]))); 

       //locations.add(line); 
      } 
     } 
    // return locations; 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      reader.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    return null; 
} 
+1

請添加額外信息的問題,而不是作爲答案 – Alfabravo

+0

這是我想要做的。創建一個應用程序以允許用戶添加自定義位置。也就是說,他們 應該能夠提供名稱,緯度/經度和時區。時區 應取自GMT的偏移量。提供的位置必須保存到 文件中,並在應用程序再次重新加載時回讀 – nadia

0

公共無效AddLocationHandler()拋出IOException異常 {

String name = ((EditText) findViewById(R.id.nameText)).getText().toString(); 
    String longi = ((EditText) findViewById(R.id.longText)).getText().toString(); 
    String lati = ((EditText) findViewById(R.id.latText)).getText().toString(); 
    String timezone = ((EditText) findViewById(R.id.timeText)).getText().toString(); 

    Double locationLong; 
    Double loctaionLat; 
    TimeZone locationTimeZone; 

    try { 
     loctaionLat = parseDouble(lati); 
    }catch (NumberFormatException e) 
    { 
     Toast toast = Toast.makeText(getApplicationContext(), "not a valid latitude ", Toast.LENGTH_SHORT); 
     toast.show(); 
     return; 


    } 
    try { 
     locationLong = parseDouble(longi); 
    }catch (NumberFormatException e) 
    { 
     Toast toast = Toast.makeText(getApplicationContext(), "not a valid longitude ", Toast.LENGTH_SHORT); 
     toast.show(); 
     return; 


    } 
    if(timezone.matches("(\\+|\\-)[0-1][0-9]\\:[034][05]")){ 
     locationTimeZone = TimeZone.getTimeZone("GMT" + timezone); 
    } 
    else 
    { 
     Toast toast = Toast.makeText(getApplicationContext(), "not a valid Time Zone ", Toast.LENGTH_SHORT); 
     toast.show(); 
     return; 
    } 
    File file = new File("au_location.txt"); 

    if(!file.exists()){ 
     file = new File(getFilesDir(), "au_location.txt"); 
    } 
    FileOutputStream fos = openFileOutput("au_locations.txt", Context.MODE_APPEND); 
    OutputStreamWriter f = new OutputStreamWriter(fos); 

    f.write(name + "," +loctaionLat + "," + locationLong + "," + "GMT" + timezone + "\n"); 
    //fos.write(s.getBytes()); 
    //fos.close(); 
    Toast.makeText(getBaseContext(),"Data Saved", Toast.LENGTH_LONG).show(); 

    f.flush(); 
    f.close(); 

    Toast.makeText(getBaseContext(),"New Location Added", Toast.LENGTH_LONG).show(); 



}