我想制作一个可以用来检查雇主向我付款的应用程序。 是一分钟吗?到半个小时?几点钟?提前一分钟超时,我会损失多少钱?
在我开始将一些代码抽象为私有方法之前,我的应用程序似乎运行良好(因此,如果需要,可以在其他地方使用它)。
但是现在它在启动时崩溃,并在“尝试从空对象引用上的字段'float com.badlogic.gdx.graphics.Color.r'中读取”上给出NullPointerException
我不明白为什么。 如果从loadPrefs()方法中删除对setTimeIn()和setClockedInLbl()的调用,该程序将运行而不会出现问题。
我做错了什么?
代码有点长...对不起! (某些功能尚未实现)
MyGDXGame.java
(正在格式化,请稍候)
public class MyGdxGame implements ApplicationListener
{
private Preferences prefs;
private Stage stage;
private TimeUtils time;
Calendar calendar;
private TextButton clockInOutBtn;
private Label earnedTodaySecondsValueLbl;
private Label earnedTodayMinutesValueLbl;
private Label earnedTodayHoursValueLbl;
private Label clockedInStatusLbl;
private Label workedForSecondsLbl;
private Label workedForMinutesLbl;
private Label workedForHoursLbl;
private Label timeWorkedToday;
private Label timeClockedInLbl;
private long timeClockedIn;
private boolean clockedIn;
private float payRate;
@Override
public void create()
{
prefs = Gdx.app.getPreferences("prefs");
clockedIn = false;
timeClockedIn = 0;
payRate = 10.14f;
time = new TimeUtils();
calendar = Calendar.getInstance();
calendar.setTimeInMillis(time.millis());
BitmapFont font = new BitmapFont();
font.setScale(2.0f);
Label.LabelStyle lStyle = new Label.LabelStyle();
lStyle.font = font;
lStyle.fontColor = Color.BLACK;
Label dayTFLbl = new Label("Day:", lStyle);
Label monthTFLbl = new Label("Month", lStyle);
Label yearTFLbl = new Label("Year", lStyle);
Label hourTFLbl = new Label("Hour", lStyle);
Label minuteTFLbl = new Label("Minute", lStyle);
Label payRateLbl = new Label("Hourly rate: ", lStyle);
Label earnedTodaySecondsLbl = new Label("Earned today by the second:", lStyle);
Label earnedTodayMinutesLbl = new Label("Earned today by the minute:", lStyle);
Label earnedTodayHoursLbl = new Label("Earned today by the hour:", lStyle);
earnedTodaySecondsValueLbl = new Label("£0.00", lStyle);
earnedTodayMinutesValueLbl = new Label("£0.00", lStyle);
earnedTodayHoursValueLbl = new Label("£0.00", lStyle);
Label earnedThisWeekLbl = new Label("Earned this week:", lStyle);
Label earnedThisWeekValueLbl = new Label("£0.00", lStyle);
Label earnedThisYearLbl = new Label("Earned this year:", lStyle);
Label earnedThisYearValueLbl = new Label("£0.00", lStyle);
clockedInStatusLbl = new Label("Not Clocked In", lStyle);
workedForSecondsLbl = new Label("Seconds worked:", lStyle);
workedForMinutesLbl = new Label("Minutes worked:", lStyle);
workedForHoursLbl = new Label("Hours worked", lStyle);
timeWorkedToday = new Label("Time worked today: hh:ss", lStyle);
timeClockedInLbl = new Label("Time clocked in: hh:mm", lStyle);
TextField.TextFieldStyle tfStyle = new TextField.TextFieldStyle();
tfStyle.font = font;
tfStyle.fontColor = Color.BLACK;
tfStyle.background = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("textfieldBGSmall.png"))));
TextField dayTF = new TextField("00", tfStyle);
dayTF.setRightAligned(true);
TextField monthTF = new TextField("00", tfStyle);
monthTF.setRightAligned(true);
TextField yearTF = new TextField("00", tfStyle);
yearTF.setRightAligned(true);
TextField hourTF = new TextField("00", tfStyle);
hourTF.setRightAligned(true);
TextField minuteTF = new TextField("00", tfStyle);
minuteTF.setRightAligned(true);
TextField payRateTF = new TextField(String.valueOf(payRate), tfStyle);
payRateTF.setRightAligned(true);
Integer minute = calendar.get(Calendar.MINUTE);
Integer hour = calendar.get(Calendar.HOUR);
Integer day = calendar.get(Calendar.DAY_OF_MONTH);
Integer month = calendar.get(Calendar.MONTH);
Integer year = calendar.get(Calendar.YEAR);
minuteTF.setText(minute.toString());
hourTF.setText(hour.toString());
dayTF.setText(day.toString());
monthTF.setText(month.toString());
yearTF.setText(year.toString());
TextButton.TextButtonStyle bStyle = new TextButton.TextButtonStyle();
bStyle.font = font;
bStyle.up = new NinePatchDrawable(new NinePatch(new Texture(Gdx.files.internal("ButtonGreyUp.png")), 10, 10, 10, 10));
bStyle.down = new NinePatchDrawable(new NinePatch(new Texture(Gdx.files.internal("ButtonGreyDown.png")), 10, 10, 10, 10));
clockInOutBtn = new TextButton("Clock In", bStyle);
clockInOutBtn.setTouchable(Touchable.enabled);
clockInOutBtn.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
clockInOut();
return false;
}
});
Table table = new Table();
table.add(dayTFLbl);
table.add(dayTF);
table.add(monthTFLbl);
table.add(monthTF);
table.add(yearTFLbl);
table.add(yearTF);
table.row();
table.add(hourTFLbl).colspan(2);
table.add(hourTF);
table.add(minuteTFLbl).colspan(2);
table.add(minuteTF);
table.row();
table.add(timeClockedInLbl).colspan(5);
table.row();
table.add(timeWorkedToday).colspan(5);
table.row();
table.add(payRateLbl).colspan(5);
table.add(payRateTF);
table.row();
table.add(earnedTodaySecondsLbl).colspan(5);
table.add(earnedTodaySecondsValueLbl);
table.row();
table.add(earnedTodayMinutesLbl).colspan(5);
table.add(earnedTodayMinutesValueLbl);
table.row();
table.add(earnedTodayHoursLbl).colspan(5);
table.add(earnedTodayHoursValueLbl);
table.row();
table.add(earnedThisWeekLbl).colspan(5);
table.add(earnedThisWeekValueLbl);
table.row();
table.add(earnedThisYearLbl).colspan(5);
table.add(earnedThisYearValueLbl);
table.row();
table.add(clockedInStatusLbl).colspan(5);
table.add(clockInOutBtn);
table.setFillParent(true);
table.setDebug(true);
stage = new Stage(new StretchViewport((Gdx.graphics.getWidth() / 2) + 100, Gdx.graphics.getHeight() / 2));
stage.addActor(table);
Gdx.input.setInputProcessor(stage);
loadPrefs();
}
private void clockInOut() {
clockedIn = !clockedIn;
if (clockedIn) {
timeClockedIn = time.millis();
setTimeIn();
}
setClockedInLbl();
}
private void setClockedInLbl() {
if (clockedIn) {
clockedInStatusLbl.setText("Clocked In");
clockInOutBtn.setText("Clock Out");
} else {
clockedInStatusLbl.setText("Clocked Out");
clockInOutBtn.setText("Clock In");
}
}
private void setTimeIn() {
long timeIn = time.millis() - timeClockedIn;
long hourIn = TimeUnit.HOURS.convert(timeIn, TimeUnit.MILLISECONDS);
long minuteIn = TimeUnit.MINUTES.convert(timeIn, TimeUnit.MILLISECONDS);
String timeInS = hourIn + ":" + (minuteIn % 60);
timeClockedInLbl.setText("Time clocked in: " + timeInS);
}
@Override
public void render()
{
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (clockedIn) {
long timeIn = time.millis() - timeClockedIn;
long hoursIn = TimeUnit.HOURS.convert(timeIn, TimeUnit.MILLISECONDS);
long minutesIn = TimeUnit.MINUTES.convert(timeIn, TimeUnit.MILLISECONDS);
long secondsIn = TimeUnit.SECONDS.convert(timeIn, TimeUnit.MILLISECONDS);
float earnedHours = hoursIn * payRate;
float earnedMinutes = minutesIn * (payRate / 60);
float earnedSeconds = secondsIn * ((payRate / 60) / 60);
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(2);
String timeWorked = hoursIn + ":" + (minutesIn % 60) + ":" + (secondsIn % 60);
timeWorkedToday.setText("Time worked today: " + timeWorked);
earnedTodaySecondsValueLbl.setText("£" + df.format(earnedSeconds));
earnedTodayMinutesValueLbl.setText("£" + df.format(earnedMinutes));
earnedTodayHoursValueLbl.setText("£" + df.format(earnedHours));
workedForSecondsLbl.setText("Seconds worked: " + secondsIn);
workedForMinutesLbl.setText("Minutes worked: " + minutesIn);
workedForHoursLbl.setText("Hours worked: " + hoursIn);
}
stage.act();
stage.draw();
}
private void loadPrefs() {
clockedIn = prefs.getBoolean("clockedIn", false);
timeClockedIn = prefs.getLong("timeClockedIn", 0);
setTimeIn();
setClockedInLbl();
}
private void savePrefs() {
prefs.putBoolean("clockedIn", clockedIn);
prefs.putLong("timeClockedIn", timeClockedIn);
prefs.flush();
}
@Override
public void dispose()
{
stage.dispose();
}
@Override
public void pause()
{
savePrefs();
}
@Override
public void resume()
{
loadPrefs();
}
@Override
public void resize(int width, int height) {}
}