TimePicker .getCurrentHour()和.getCurrentMinute()在api 19中返回null

时间:2017-11-21 01:50:41

标签: java android timepicker

您好我已经陷入这个问题一段时间了,我在堆栈溢出和其他网站上尝试了很多解决方案,但似乎没有什么对我有效。

以下是java代码:

    public void buttonClicked() {
    final TimePicker time = (TimePicker) this.findViewById(R.id.time_picker);
    final CheckBox repeat_daily = (CheckBox) this.findViewById(R.id.repeat_daily);


    AlertDialog.Builder dialog;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        dialog = new AlertDialog.Builder(AgendaActivity.this, android.R.style.Theme_Material_Light_Dialog_Alert);
    } else {
        dialog = new AlertDialog.Builder(AgendaActivity.this);
    }


    dialog.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog,
                                    int which) {

                    time.clearFocus();
                    int hour = time.getCurrentHour();
                    int minute = time.getCurrentMinute();

                    Notification.getTime(hour, minute, repeat_daily.isChecked());
                    Notification.scheduleNotification(AgendaActivity.this, 1);
                }
            });

    dialog.setNegativeButton("CANCEL",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog,
                                    int which) {
                    dialog.dismiss();
                }
            });
    View pickers = getLayoutInflater().inflate(R.layout.time_picker,
            null);
    dialog.setView(pickers);
    dialog.show();
}

这是.xml:

   <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical">


    <TimePicker
        android:id="@+id/time_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:numbersBackgroundColor="@color/white"
        android:numbersSelectorColor="@color/colorPrimaryDark"
        android:tag="11"
        android:timePickerMode="spinner"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/repeat_daily"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:fontFamily="casual"
        android:gravity="center"
        android:text="Repeat Daily"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/time_picker" />

</android.support.constraint.ConstraintLayout>

这是它返回的错误:尝试在空对象引用上调用虚方法“void android.widget.TimePicker.clearFocus()

如果您需要更多信息,请告诉我

PS:是的,我试过getHour(),我稍后会进行api检查,但这不是导致问题的原因。

1 个答案:

答案 0 :(得分:0)

您没有正确地投射膨胀布局的视图,因此它返回空值。这样做

public void ButtonClick()
{

    AlertDialog.Builder dialog=new AlertDialog.Builder(this);
    LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View pickers=layoutInflater.inflate(R.layout.time_picker,null);
    final TimePicker time = (TimePicker)pickers.findViewById(R.id.time_picker);
    final CheckBox repeat_daily = (CheckBox) pickers.findViewById(R.id.repeat_daily);
    dialog.setView(pickers);
    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            time.clearFocus();
            int hour = time.getCurrentHour();
            int minute = time.getCurrentMinute();

            Notification.getTime(hour, minute, repeat_daily.isChecked());
            Notification.scheduleNotification(AgendaActivity.this, 1);
        }
    });
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            dialog.dismiss();
        }
    });

    AlertDialog alertDialog=dialog.create();
    alertDialog.show();

}

这应该可以正常工作。