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