有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使TimePickerDialog等待输入;将运行结果后的代码写入NullReferenceException

我遇到了一个我已经挣扎了一段时间的问题。我创建了一个片段类TimePickerFragment,它扩展了DialogFragment,在MainActivity中调用它来显示对话框。在显示TimePicker之后,它应该等待用户输入,否则代码会在它运行后直接启动IntentService。这将导致NullReferenceException,因为enteredTime为空。顺便说一句,我之所以调用button.PerformClick(),是因为我直接想在用户点击通知后显示TimePicker

我对Android相当陌生,对Java只有相当好的了解。可能有一些我完全不知道的东西。谢谢

{}的设置如下:

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

}


共 (1) 个答案

  1. # 1 楼答案

    最好的方法是,在通知中捆绑一些数据,并在创建活动时检查数据(如果通知数据存在),然后直接打开时间选择器

    要在通知中捆绑数据,请执行以下操作:

            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
    

    希望它能帮助你:)