0

Hello I've been stuck in this problem for a while, I tried a lot of solutions both here in stack overflow and in other sites but nothing seems to work for me.

Here is the java code:

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

And here is the .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>

This is the error that it returns: Attempt to invoke virtual method 'void android.widget.TimePicker.clearFocus()' on a null object reference

Please tell me if you need any more information

PS: Yes, I tried getHour(), I will do the api checks later, but that's not what is causing the issue.

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
  • Those methods aren't returning null. It's not even getting that far. `time` is null, which means `this.findViewById(R.id.time_picker)` is returning null. If the layout you've shown is the `time_picker` layout you're using for the `Dialog`, then you're not finding the `View`s correctly. – Mike M. Nov 21 '17 at 02:02

1 Answers1

0

You are not casting the views of inflated layout correctly, so its returning null value. Do like this

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

}

this should work fine.

Mohammed Farhan
  • 1,120
  • 8
  • 14