私はすでにしばらくの間苦労している問題があります。 FragmentクラスTimePickerFragment
を作成しました。これはDialogFragment
を拡張し、MainActivity
で呼び出されてダイアログを表示します。 TimePicker
が表示されたら、ユーザー入力を待つ必要があります。それ以外の場合は、コードを直接実行した後、IntentService
を開始します。 enteredTime
がヌルなので、これはNullReferenceException
になります。 Btw、私はbutton.PerformClick()
に電話しています。ユーザーが通知をクリックした後に直接TimePicker
を表示したいからです。TimePickerDialogを入力待ちにする。実行後のコードはNullReferenceExceptionになります
私は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);
}
}
:
のonCreateで今データだけをチェック:通知内のデータをバンドルに
は、その質問に関係する。 NullReferenceExceptionは、TimePickerDialogがユーザーの入力を待機しないために発生します。そのため、TimePickerの期待値がnullであり、例外が発生します。 Questionは、TimePickerを入力待ちにする方法です。 –