我是android的新手,我对java程序感到有些困惑。在我的程序中,我创建了4个时间选择器,用于获取时间和超时。之后,editText将显示总小时数。
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date dateb=null;
Date datec=null;
Date dateb1=null;
Date datec1=null;
Date dateb2=null;
Date datec2=null;
Date dateb3=null;
Date datec3=null;
try {
dateb = format.parse(b);
datec = format.parse(c);
long difference = datec.getTime() - dateb.getTime();
int minutes = (int) ((difference / (1000 * 60)) % 60);
int hours = (int) ((difference / (1000 * 60 * 60)) % 24) - 1;
editTextH1.setText((hours + ":" + minutes));
} catch (Exception e) {
System.err.println("ouch!");
}
try {
dateb1 = format.parse(d);
datec1 = format.parse(e1);
long difference1 = datec1.getTime() - dateb1.getTime();
int minutes1 = (int) ((difference1 / (1000 * 60)) % 60);
int hours1 = (int) ((difference1 / (1000 * 60 * 60)) % 24) - 1;
editTextH2.setText((hours1 + ":" + minutes1));
} catch (Exception e) {
System.err.println("ouch!");
}
try {
dateb2 = format.parse(f);
datec2 = format.parse(g);
long difference2 = datec2.getTime() - dateb2.getTime();
int minutes2 = (int) ((difference2 / (1000 * 60)) % 60);
int hours2 = (int) ((difference2 / (1000 * 60 * 60)) % 24) - 1;
editTextH3.setText((hours2 + ":" + minutes2));
} catch (Exception e) {
System.err.println("ouch!");
}
try {
dateb3 = format.parse(h);
datec3 = format.parse(i);
long difference3 = datec3.getTime() - dateb3.getTime();
int minutes3 = (int) ((difference3 / (1000 * 60)) % 60);
int hours3 = (int) ((difference3 / (1000 * 60 * 60)) % 24) - 1;
editTextH4.setText((hours3 + ":" + minutes3));
}catch (Exception e) {
System.err.println("ouch!");
}
// display total hours
long dateb_sum = dateb.getTime() + dateb1.getTime() + dateb2.getTime()+dateb3.getTime();
long datec_sum = datec.getTime() + datec1.getTime() + datec2.getTime()+datec3.getTime();
long difference4 = datec_sum - dateb_sum;
int minutes4 = (int) ((difference4/ (1000*60)) % 60);
int hours4 = (int) ((difference4/ (1000*60*60)) % 24)-1;
editText8.setText((hours4+":"+minutes4));
}
}
当我运行我的应用程序时,它在时间选择器上崩溃并且项目被迫停止。我已经初始化了所有的editText但仍然出现此错误。有人可以帮帮我吗?感谢
logcat的
10-01 01:21:13.041 4552-4552/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 4552
java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference
at com.example.project.project.WorkDetailsTable$TimePick.onTimeSet(WorkDetailsTable.java:381)
at android.app.TimePickerDialog.onClick(TimePickerDialog.java:145)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我很确定错误来自下面的编码,因为当我删除它时它不会崩溃。但是我需要它们来计算总的小时数!!
long dateb_sum = dateb.getTime() + dateb1.getTime() + dateb2.getTime()+dateb3.getTime();
long datec_sum = datec.getTime() + datec1.getTime() + datec2.getTime()+datec3.getTime();
long difference4 = datec_sum - dateb_sum;
int minutes4 = (int) ((difference4/ (1000*60)) % 60);
int hours4 = (int) ((difference4/ (1000*60*60)) % 24)-1;
editText8.setText((hours4+":"+minutes4));
答案 0 :(得分:3)
除了崩溃,我认为你使用了很多重复的代码,你可以清理它。使用以下方法。在Params中传递EditText和日期,并根据需要调用。
setMyTime(EditText editText,String date1, String date2){
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
try {
Date d1 = format.parse(date1);
Date d2 = format.parse(date2);
long difference = d1.getTime() - d2.getTime();
int minutes = (int) ((difference / (1000 * 60)) % 60);
int hours = (int) ((difference / (1000 * 60 * 60)) % 24) - 1;
editText.setText((hours + ":" + minutes));
}catch (Exception e) {
System.err.println(e.printStackTrace());
}
}
示例:
setMyTime(editTextH1,b,c);
setMyTime(editTextH2,d,e);
setMyTime(editTextH3,f,g);
setMyTime(editTextH4,h,i);