Java 无法启动活动,行读取器中的ArrayIndexOutOfBounds
我一直收到一个Java 无法启动活动,行读取器中的ArrayIndexOutOfBounds,java,android,Java,Android,我一直收到一个ArrayIndexOutOfBoundsException,但我不确定到底是哪部分代码导致了这个错误。任何帮助都将不胜感激。代码是 while ((line = reader.readLine()) != null) { String[] wordOnLine = line.split(","); geo.put(wordOnLine[0] , new GeoLocation(wordOnLine[0], Doub
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:长度=1;索引=1
公共列表getListOfLocations(){
公共列表getListOfLocations(){
public void AddLocationHandler()引发IOException {
public void AddLocationHandler()引发IOException {
请在问题中添加额外信息,而不是作为答案。这是我正在尝试做的。创建一个应用程序,允许用户添加自定义位置。也就是说,他们应该能够提供名称、纬度/经度和时区。时区应以格林尼治标准时间的偏移量获取。提供的位置必须保留到文件中,然后重新读取重新加载应用程序时,请在问题中添加额外信息,而不是作为答案。这是我正在尝试做的。创建一个应用程序,允许用户添加自定义位置。也就是说,他们应该能够提供名称、纬度/经度和时区。时区应作为GMT的偏移量获取。提供的位置必须是保存到一个文件,并在重新加载应用程序时重新读取。您正在按-“,”分隔行。然后您正在参考wordOnLine-您确定每行都以coma结尾吗?请检查此项:谢谢,您的答复。是的,每行都需要有camma,因为我必须从文件中读取以下文本。Glenmore Park,-33.79068150.6693,澳大利亚/Sydney@nadia我想你需要有2个循环,1.按
\n
每行拆分,2.循环到行。长度按每行拆分文本,
按-','拆分行。然后你参考wordOnLine-你确定每行都以昏迷结尾吗?同时检查这一行:谢谢你的回复。是的,每一行都需要有camma,因为我必须从文件中读取以下文本。Glenmore Park,-33.79068150.6693,澳大利亚/Sydney@nadia我认为您需要2个循环,1.按\n
拆分每行,2.循环到行。长度按拆分每行文本,
// 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;
}
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();
}