Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8295721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:42:38+00:00 2026-06-08T14:42:38+00:00

So my app has 2 buttons. One that opens a datepicker dialog fragment and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:42:40+00:00Added an answer on June 8, 2026 at 2:42 pm

    When updating time for Calendar, it should be as follows:

    public void updateChangedTime(int hourOfDay, int minute) {
        buttonTime.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
        now.set(Calendar.HOUR_OF_DAY, hourOfDay);
        now.set(Calendar.MINUTE, minute);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm making an android app that has more than 100 buttons,but you know
The app we're building has a simple button that starts a facetime session with
My app has several buttons which trigger different events. The user should NOT be
My swing app has a main window which has sever buttons. This JFrame is
I'm writing a Rails app which has the following jasmine spec: describe buttons, ->
I have a winforms app and the main (and only) form has several buttons.
I have an view in my App which has a number of buttons based
I have a page X in my grails app which has a button to
I have made an iPhone App which has a loginwithFacebook button , the login
I have a small data collection app which has a single button to start

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.