I made a code to fetch information from an text file, but it always fails at getting data from the array. If I manually put in a number it works, otherwise it crashes.
Here the code:
public void reload () {
File file = new File(((Context)this).getExternalFilesDir(null), "Worktime.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
Log.e("tk.spyfly.Worktime", "Unable to read the Worktime.txt file.");
}
Scanner s = new Scanner(text.toString());
list.clear();
while (s.hasNext()){
list.add(s.next());
}
s.close();
String current;
int worktimetoday=0;
int i = list.size();
int count = 0;
while (count != i){
count= count +1;
current = list.get(count);
current.replace(getString(R.string.workstart) + " ","");
current.replace(getString(R.string.workstop) + " ","");
Calendar cs = Calendar.getInstance();
cs.getTime();
int year = cs.YEAR;
int month = cs.MONTH;
int day = cs.DAY_OF_MONTH;
Date date;
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
try {
date = formatter.parse(current);
cs.setTime(date);
} catch (ParseException e) {
e.printStackTrace();
}
int workstart = cs.DAY_OF_MONTH;
int workyear = cs.YEAR;
int workmonth = cs.MONTH;
int workstartmin = cs.MINUTE;
int workstarth = cs.HOUR_OF_DAY;
if ((count+1) != i){
count=count+1;
if (workyear == year & workmonth == month & day == workstart){
current = list.get(count);
current.replace(getString(R.string.workstart) + " ","");
current.replace(getString(R.string.workstop) + " ","");
try {
date = formatter.parse(current);
cs.setTime(date);
} catch (ParseException e) {
e.printStackTrace();
}
int workend = cs.DAY_OF_YEAR;
int workendmin = cs.MINUTE;
int workendh = cs.HOUR_OF_DAY;
if (workend==workstart){
int workendtime = workendh * 60 + workendmin;
int workstarttime = workstarth * 60 + workstartmin;
worktimetoday = worktimetoday + (workendtime - workstarttime);
}else {
int workstarttime = workstarth * 60 + workstartmin;
worktimetoday = worktimetoday + (1440 - workstarttime);
}}}}
TextView tw = (TextView) findViewById(R.id.textView);
tw.setText(getString(R.string.worked)+ " "+ worktimetoday + " min.");
}
This is the console error:
04-16 20:51:02.420 19544-19544/tk.spyfly.worktime E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: tk.spyfly.worktime, PID: 19544
java.lang.RuntimeException: Unable to start activity ComponentInfo{tk.spyfly.worktime/tk.spyfly.worktime.Overview}: java.lang.IndexOutOfBoundsException: Invalid index 42, size is 42
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 42, size is 42
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at tk.spyfly.worktime.Overview.reload(Overview.java:79)
at tk.spyfly.worktime.Overview.onCreate(Overview.java:164)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
0 comments:
Post a Comment