So my app has 2 buttons. One that opens a datepicker dialog fragment and one that opens a timepicker fragment. The button texts show the selected date and time. The date picker works perfectly but I’m running into several problems with the timepicker, which may all be related.
Issue 1: The picker is set to 12 hour time, and Clicking on AM or PM to switch between them causes the app to crash (logcat will be pasted below).
Issue 2: After selecting a new time, if I click the button again, the timepicker is sometimes showing the current time instead of the last selected time. Sometimes it’s showing a completely different hour altogether (eg. I click it at 11.00, and change it to 10.00. When I click the button again, the timepicker is set to 6.00pm). I haven’t found a way of repeating this behaviour.
Issue 3: sometimes the AM/PM button is on the wrong selection even though the time is correct (hour and minute) is correct.
Logcat
07-29 23:59:03.483: E/Trace(2167): error opening trace file: No such file or directory (2)
07-29 23:59:12.703: E/AndroidRuntime(2167): FATAL EXCEPTION: main
07-29 23:59:12.703: E/AndroidRuntime(2167): java.lang.ArrayIndexOutOfBoundsException: length=17; index=23
07-29 23:59:12.703: E/AndroidRuntime(2167): at java.util.Calendar.set(Calendar.java:1210)
07-29 23:59:12.703: E/AndroidRuntime(2167): at com.rone.datetimepractice.PickerActivity$2.updateChangedTime(PickerActivity.java:71)
07-29 23:59:12.703: E/AndroidRuntime(2167): at com.rone.datetimepractice.TimeDialogFragment$1.onTimeSet(TimeDialogFragment.java:46)
07-29 23:59:12.703: E/AndroidRuntime(2167): at android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java:130)
07-29 23:59:12.703: E/AndroidRuntime(2167): at android.app.TimePickerDialog.onClick(TimePickerDialog.java:115)
07-29 23:59:12.703: E/AndroidRuntime(2167): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
07-29 23:59:12.703: E/AndroidRuntime(2167): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 23:59:12.703: E/AndroidRuntime(2167): at android.os.Looper.loop(Looper.java:137)
07-29 23:59:12.703: E/AndroidRuntime(2167): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-29 23:59:12.703: E/AndroidRuntime(2167): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 23:59:12.703: E/AndroidRuntime(2167): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 23:59:12.703: E/AndroidRuntime(2167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-29 23:59:12.703: E/AndroidRuntime(2167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-29 23:59:12.703: E/AndroidRuntime(2167): at dalvik.system.NativeStart.main(Native Method)
Code surrounding Line 71 from PickerActivity – exact line is the “now.set(hourOfDay, minute);”
public void showDialogTime() {
FragmentTransaction ftTime = getFragmentManager().beginTransaction();
fragTime = TimeDialogFragment.newInstance(this, new TimeDialogFragmentListener() {
public void updateChangedTime(int hourOfDay, int minute) {
buttonTime.setText(String.valueOf(hourOfDay)+":"+String.valueOf(minute));
now.set(hourOfDay, minute);
}}, now);
fragTime.show(ftTime, "TimeDialogFragment");
}
Code surrounding Line 46 from TimeDialogFragment – specifically the mListener line
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
mListener.updateChangedTime(hourOfDay, minute);
}
Here is the full PickerActivity.java file minus the datepicker sections:
public class PickerActivity extends Activity {
TimeDialogFragment fragTime;
Button buttonDate, buttonTime;
Calendar now;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
now = Calendar.getInstance();
buttonTime = (Button) findViewById(R.id.time_button);
buttonTime.setText(String.valueOf(now.get(Calendar.HOUR))+":"+
String.valueOf(now.get(Calendar.MINUTE)));
}
public void clickMe(View v) {
int id = v.getId();
switch (id) {
case R.id.date_button :
showDialogDate();
break;
case R.id.time_button :
showDialogTime();
break;
default:
break;
}}
public interface TimeDialogFragmentListener {
public void updateChangedTime(int hourOfDay, int minute);
}
public void showDialogTime() {
FragmentTransaction ftTime = getFragmentManager().beginTransaction();
fragTime = TimeDialogFragment.newInstance(this, new TimeDialogFragmentListener() {
public void updateChangedTime(int hourOfDay, int minute) {
buttonTime.setText(String.valueOf(hourOfDay)+":"+String.valueOf(minute));
now.set(hourOfDay, minute);
}}, now);
fragTime.show(ftTime, "TimeDialogFragment");
}}
Here is the TimeDialogFragment.java file:
public class TimeDialogFragment extends DialogFragment {
static Context mContext;
static int mHour;
static int mMinute;
static TimeDialogFragmentListener mListener;
public static TimeDialogFragment newInstance(Context context,
TimeDialogFragmentListener listener, Calendar now) {
TimeDialogFragment dialog = new TimeDialogFragment();
mContext = context;
mListener = listener;
mHour = now.get(Calendar.HOUR);
mMinute = now.get(Calendar.MINUTE);
Bundle args = new Bundle();
args.putString("title", "Set Time");
dialog.setArguments(args);
return dialog;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new TimePickerDialog(mContext, mTimeSetListener, mHour, mMinute, false);
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
mListener.updateChangedTime(hourOfDay, minute);
}};}
If it makes any difference to how we tackle this, I do intend to save the time into my database so I can sort results by time, as well as allow the user to change the time on any database row/record to whatever time they choose. I’d also like to include the AM/PM text on the button, but I’m happy to tackle these issues myself once the above issues are resolved. Any help is appreciated.
Just in addition to the above, if I change
return new TimePickerDialog(mContext, mTimeSetListener, mHour, mMinute, false);
to
return new TimePickerDialog(mContext, mTimeSetListener, mHour, mMinute, true);
in the TimeDialogFragment.java file, it crashes immediately when selecting an hour in the opposite AM/PM bracket, eg. It’s 0.50 (12.50am) now, but If I select 23.50 (11.50pm) it crashes with the same logcat error as above. Changing to another AM time doesn’t cause any problems.
When updating time for Calendar, it should be as follows: