我不断获得ArrayIndexOutOfBoundsException
,但我不确定代码的哪一部分确实会导致此错误。任何帮助将不胜感激。代码是
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:length = 1;索引= 1
答案 0 :(得分:0)
public List 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 :(得分:0)
public void 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();
}