0

I got an issue I that have been struggling already for a while. I created a Fragment class, TimePickerFragment, that extends DialogFragment which is being called in the MainActivity to show the Dialog. After the TimePicker is shown it should wait for user input, otherwise the code after it runs directly, starting an IntentService. This will then results in a NullReferenceException because enteredTime is null. Btw, I am calling the the button.PerformClick() because I directly want to show the the TimePicker after a user clicked on a notification.

I'm fairly new to Android, and have only decent knowledge in Java. There might be something that I am missing completely because simply don't know. Thanks!

The MainActivity is set up like this:

public void onCreate(Bundle savedInstanceState) {    

if (btnShowTimePickerDialog != null) {
            btnShowTimePickerDialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showTimePickerDialog(v);
                }
            });
        }
final ResultReceiver verifyTokenReceiver = new ResultReceiver(new Handler()) {
            @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
               if (resultData == null) {
                    return;
                }

            if (resultCode == Keys.SUCCESS_CODE) {
                    //TODO: GET VALUES FROM TIMEPICKER
                if (previousActivity.equals(Keys.MAINSERVICE_CODE)){

                    btnShowTimePickerDialog.performClick();


                    final Intent createSessionIntent = new Intent(MainActivity.this, CreateSessionService.class);
                    createSessionIntent.putExtra(Keys.RECEIVER_KEY, createSessionReceiver);
                    createSessionIntent.putExtra(Keys.TOKEN_KEY, token);
                    createSessionIntent.putExtra(Keys.PREFERENCE_KEY, preference);
                    createSessionIntent.putExtra(Keys.ACTIVITY_KEY, previousActivity);
                    createSessionIntent.putExtra(Keys.TIME_KEY, enteredTime);//insert time
startService(createSessionIntent);
}

 public void showTimePickerDialog(View v) {
    FragmentManager mFragmentManager = getSupportFragmentManager();
    TimePickerFragment tpf = new TimePickerFragment();
    tpf.show(mFragmentManager, "TimePickerDialog");

}

The TimePickerDialog has been created in a Fragment:

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
TimePickerDialog.OnTimeSetListener mCallback;
public TimePickerFragment() {  super(); }

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 = c.get(Calendar.MINUTE);

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


@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    ((MainActivity)getActivity()).SomeRandomMethodToGetTheEnteredTime(hourOfDay, minute);
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (TimePickerDialog.OnTimeSetListener) activity;
    } catch (Exception e) {
        throw new ClassCastException(activity.toString() + " must implement TimePickerDialog.OnTimeSetListener");
    }
}

@Override
public void show(FragmentManager manager, String tag) {
    super.show(manager, tag);
}

}

  • I think the duplicate doesn't relate to the question. The NullReferenceException is caused because the TimePickerDialog doesn't wait for user input, therefore, the expected value from the TimePicker is null and causes the exception. Question is how to make the TimePicker wait for the input. – Jan-Niklas Schneider Jun 14 '16 at 10:58

1 Answers1

0

The best approach will be, bundle some data in notification and in onCreate of the activity check the data if the notification data exist then open your time picker directly.

To bundle data in notification :

        Bundle bundle = new Bundle();
        bundle.putBoolean("NOTIF", true);
        resultIntent.putExtra("DATA", bundle);

Now in onCreate just check for the data :

if(resultIntent.hasExtra("DATA")){
       Bundle notifBundle = getIntent().getBundleExtra("DATA");
       if(notifBundle.getBoolean("NOTIF"))
          //show your time picker

Hope it will help you :)

Neo
  • 3,546
  • 1
  • 24
  • 31
  • Thanks for your comment. Maybe the flow i described wasn't 100% clear. The notification is opening the TimePickerDialog immediately after it has been used. It also runs the code that follows after it, which expects user input. The behavior though is the TimePicker shows up and code behind it runs with a null value. So, somehow the TimePicker should wait for user input. – Jan-Niklas Schneider Jun 14 '16 at 11:02