javaandroidandroid-dialogfragmentandroid-timepicker

Why am I getting a a null object reference error?


I am trying to show a custom minute interval in the time picker, so here is what I'm trying to do. In my MainActivity I have this method(called on a button click),which is opening a dDialogFragment:

public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }

Here is the TimePickerFragment:

public class TimePickerFragment extends DialogFragment
        implements DurationTimePickDialog.OnTimeSetListener {


    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = 0;

        // Create a new instance of CustomTimePickerDialog and return it
        return new CustomTimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        String realMinute= "";

        switch (minute) {
            case 1:
                realMinute = "01";
                break;
            case 2:
                realMinute = "02";
                break;
            case 3:
                realMinute = "03";
                break;
            case 4:
                realMinute = "04";
                break;
            case 5:
                realMinute = "05";
                break;
            case 6:
                realMinute = "06";
                break;
            case 7:
                realMinute = "07";
                break;
            case 8:
                realMinute = "08";
                break;
            case 9:
                realMinute = "09";
                break;
            default:
                realMinute = String.valueOf(minute);
                break;
        }

        final String selectedDate = String.valueOf(hourOfDay) + ":" + realMinute;
        EditText selectedTimeTxt = (EditText) getActivity().findViewById(R.id.selectedTime);
        selectedTimeTxt.setText(selectedDate);
    }
}

As you can see I'm using a customized class called CustomTimePickerDialog in order to show the dialog with the 10 minute interval. Here the code is:

public class CustomTimePickerDialog extends TimePickerDialog {

    private final static int TIME_PICKER_INTERVAL = 5;
    private TimePicker timePicker;
    private final OnTimeSetListener callback;

    public CustomTimePickerDialog(Context context, OnTimeSetListener callBack,
                                  int hourOfDay, int minute, boolean is24HourView) {
        super(context, callBack, hourOfDay, minute / TIME_PICKER_INTERVAL,
                is24HourView);
        this.callback = callBack;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (callback != null && timePicker != null) {
            timePicker.clearFocus();
            callback.onTimeSet(timePicker, timePicker.getCurrentHour(),
                    timePicker.getCurrentMinute() * TIME_PICKER_INTERVAL);
        }
    }

    @Override
    protected void onStop() {
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        try {
            Class<?> classForid = Class.forName("com.android.internal.R$id");
            Field timePickerField = classForid.getField("timePicker");
            this.timePicker = (TimePicker) findViewById(timePickerField
                    .getInt(null));
            Field field = classForid.getField("minute");

            NumberPicker mMinuteSpinner = (NumberPicker) timePicker
                    .findViewById(field.getInt(null));
            mMinuteSpinner.setMinValue(0);
            mMinuteSpinner.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
            List<String> displayedValues = new ArrayList<String>();
            for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
                displayedValues.add(String.format("%02d", i));
            }
            mMinuteSpinner.setDisplayedValues(displayedValues
                    .toArray(new String[0]));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

The problem is that I'm getting this strange for me error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.NumberPicker.setMinValue(int)' on a null object reference regarding this line: mMinuteSpinner.setMinValue(0); - in the CustomTimePickerDialog class.

I know that I'm messing the things here(probably in the TimePickerFragment class), but as an android and java beginner I'm not able to spot my mistake.

Can you give me a clue?


Solution

  • NumberPicker mMinuteSpinner = (NumberPicker) timePicker
                    .findViewById(field.getInt(null));
    

    is presumably returning null.