所以我有 4 个双数组,通过它们更新计算。当一个人点击一个按钮时,这 4 个双数组会自我更新。st 数组表示开始时间,et 数组表示结束时间、数量和酒精含量数组。当我按下喝啤酒时,开始时间数组会更新,数量和酒精含量数组也会更新。当我按下 Finish Beer 时,结束时间数组会更新。计算在 Runnable 中完成。没关系,但问题是当我按下喝啤酒按钮时,我得到了一个 NUllPO 异常。我正在从 runonUIThread 调用 Timer_Tick。有什么建议么 ?
private Runnable Timer_Tick = new Runnable() {
public void run() {
calculateBAC();
}
private void calculateBAC() {
int j=0;
while(et[j]!=0 && j<19) {
BAC=quantity[j]*alcoholcontent[j];
BAC=BAC*100*1.055;
if(gender=="female")
BAC=BAC/(weight*0.55);
else
BAC=BAC/(weight*0.68);
c=Calendar.getInstance();
CurrentTime=c.getTimeInMillis();
c.clear();
CurrentTime=CurrentTime-st[j];
CurrentTime=CurrentTime/(1000*360);
BAC=BAC-(CurrentTime*0.015);
TotalBAC=TotalBAC+BAC;
tv1.setText(TotalBAC+"%");
j++;
}
}
};
……
public void onClick(View arg0) {
switch(arg0.getId()) {
case R.id.drinkbeer:
if(beerstart==0) {
beerstart=1;
Log.d("worked","this worked");
c=Calendar.getInstance();
st[i]=c.getTimeInMillis();
st[i]=(st[i]*0.001)/360;
c.clear();
quantity[i]=12;
alcoholcontent[i]=0.06;
beer.setText("Finish beer");
break;
}
}
错误:
07-09 04:57:27.666: E/AndroidRuntime(12740): FATAL EXCEPTION: main
07-09 04:57:27.666: E/AndroidRuntime(12740): java.lang.NullPointerException
07-09 04:57:27.666: E/AndroidRuntime(12740): at com.example.baccalculator.bloodlevelcalculator.onClick(bloodlevelcalculator.java:120)
07-09 04:57:27.666: E/AndroidRuntime(12740): at android.view.View.performClick(View.java:4204)
07-09 04:57:27.666: E/AndroidRuntime(12740): at android.view.View$PerformClick.run(View.java:17355)
07-09 04:57:27.666: E/AndroidRuntime(12740): at android.os.Handler.handleCallback(Handler.java:725)
07-09 04:57:27.666: E/AndroidRuntime(12740): at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 04:57:27.666: E/AndroidRuntime(12740): at android.os.Looper.loop(Looper.java:137)
07-09 04:57:27.666: E/AndroidRuntime(12740): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-09 04:57:27.666: E/AndroidRuntime(12740): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 04:57:27.666: E/AndroidRuntime(12740): at java.lang.reflect.Method.invoke(Method.java:511)