0

所以我的应用程序有 2 个按钮。一个打开日期选择器对话框片段,另一个打开时间选择器片段。按钮文本显示选定的日期和时间。日期选择器工作得很好,但我遇到了时间选择器的几个问题,这些问题可能都相关。问题 1:选择器设置为 12 小时时间,单击 AM 或 PM 在它们之间切换会导致应用程序崩溃(logcat 将粘贴在下面)。问题2:选择新时间后,如果再次单击按钮,时间选择器有时会显示当前时间而不是上次选择的时间。有时它会显示完全不同的时间(例如,我在 11 点单击它,然后将其更改为 10 点。当我再次单击该按钮时,时间选择器设置为下午 6 点)。我还没有找到重复这种行为的方法。问题 3:

日志猫

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)

PickerActivity 中第 71 行周围的代码- 确切的行是“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");
}

TimeDialogFragment 中第 46 行周围的代码- 特别是 mListener 行

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        mHour = hourOfDay;
        mMinute = minute;
        mListener.updateChangedTime(hourOfDay, minute);
    }

这是完整的 PickerActivity.java 文件减去 datepicker 部分:

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");
}}

这是 TimeDialogFragment.java 文件:

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);
    }};}

如果这对我们解决此问题的方式有任何影响,我确实打算将时间保存到我的数据库中,以便我可以按时间对结果进行排序,并允许用户将任何数据库行/记录上的时间更改为他们选择的任何时间. 我还想在按钮上包含 AM/PM 文本,但一旦上述问题得到解决,我很乐意自己解决这些问题。任何帮助表示赞赏。

除了上述之外,如果我改变

return new TimePickerDialog(mContext, mTimeSetListener, mHour, mMinute, false);

return new TimePickerDialog(mContext, mTimeSetListener, mHour, mMinute, true);

在 TimeDialogFragment.java 文件中,当在相反的 AM/PM 括号中选择一个小时时,它会立即崩溃,例如。现在是 0.50(上午 12.50),但如果我选择 23.50(下午 11.50),它会因与上述相同的 logcat 错误而崩溃。更改为另一个上午时间不会导致任何问题。

4

1 回答 1

1

更新日历的时间时,应该如下:

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);
}
于 2012-07-29T15:43:32.053 回答