我遇到了一个问题,我已经挣扎了一段时间了。我创建了一个Fragment类TimePickerFragment
,它扩展了在DialogFragment
中调用的MainActivity
以显示对话框。显示TimePicker
后,它应该等待用户输入,否则直接运行后的代码,开始IntentService
。这将导致NullReferenceException
,因为enteredTime
为空。顺便说一句,我打电话给button.PerformClick()
,因为我直接想在用户点击通知后显示TimePicker
。
我对Android很新,并且只有很好的Java知识。可能有些东西我完全缺失,因为根本就不知道。谢谢!
MainActivity
设置如下:
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");
}
TimePickerDialog已在片段中创建:
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);
}
}
答案 0 :(得分:0)
最好的方法是,在通知中捆绑一些数据,并在活动的onCreate中检查数据是否存在通知数据,然后直接打开您的时间选择器。
要在通知中捆绑数据:
Bundle bundle = new Bundle();
bundle.putBoolean("NOTIF", true);
resultIntent.putExtra("DATA", bundle);
现在在onCreate中只检查数据:
if(resultIntent.hasExtra("DATA")){
Bundle notifBundle = getIntent().getBundleExtra("DATA");
if(notifBundle.getBoolean("NOTIF"))
//show your time picker
希望它会对你有所帮助:)。