gpt4 book ai didi

java - 尝试调用虚拟方法 'long java.util.Date.getTime()'

转载 作者:搜寻专家 更新时间:2023-11-01 08:40:44 25 4
gpt4 key购买 nike

<分区>

我是 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));

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com