代码之家  ›  专栏  ›  技术社区  ›  Matheus Patrick

计时器。getCurrentHour()和。getCurrentMinute()在api 19中返回null[重复]

  •  0
  • Matheus Patrick  · 技术社区  · 8 年前

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

    以下是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 回复  |  直到 8 年前
        1
  •  0
  •   Mohammed Farhan    8 年前

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

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