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

TimePicker .getCurrentHour() and .getCurrentMinute() returning null in api 19

你好,我被这个问题困扰了一段时间,我在堆栈溢出和其他网站上尝试了很多解决方案,但似乎没有一个对我有用。

这里是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>

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

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

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

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

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

}

这应该可以正常工作。