1

我有以下 DialogFragment 来选择日期:

public class Fragment_Picker_Time extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

int hour;
int minute;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

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

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
}
}

这就是我从另一个片段中调用它的方式:

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.btn_call_time:
        DialogFragment newFragment = new Fragment_Picker_Time();
        newFragment.show(getFragmentManager(), "timePicker");
        break;
    }
}

但是我不知道如何从 DialogFragment 获取值并将它们传递给我的 Fragment。我尝试了以下方法:

在 DialogFragment 中,我设置了 onTimeSet 函数:

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    Intent intent = new Intent(getParentFragment().getActivity(), Fragment_Schedules.class);
    intent.putExtra("HOUR", hour);
    intent.putExtra("MINUTE", minute);
    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
}

添加以下行来调用此片段:

case R.id.btn_call_time:
        DialogFragment newFragment = new Fragment_Picker_Time();
        newFragment.setTargetFragment(this, 55);
        newFragment.show(getFragmentManager(), "timePicker");
        break;
    }

并设置一个OnActivityResult功能:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    getActivity();
    //super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==Activity.RESULT_OK)
    {
        switch(requestCode)
        {
        case 55:
            int hour   = data.getExtras().getInt("HOUR");
            int minute = data.getExtras().getInt("MINUTE");
            Toast.makeText(getActivity(), "The selected time is: "+hour+":"+minute, Toast.LENGTH_LONG).show();
            break;
        }
    } else {
        // handle error
    }
}

但是当我尝试运行它时,我的应用程序崩溃了。这是我在 logcat 中得到的:

09-29 10:06:43.666: E/AndroidRuntime(14088): FATAL EXCEPTION: main
09-29 10:06:43.666: E/AndroidRuntime(14088): java.lang.NullPointerException
09-29 10:06:43.666: E/AndroidRuntime(14088):    at com.myname.myproject.fragments.
    Fragment_Picker_Time.onTimeSet(Fragment_Picker_Time.java:32)
09-29 10:06:43.666: E/AndroidRuntime(14088):    at android.app.TimePickerDialog
    .onClick(TimePickerDialog.java:119)
09-29 10:06:43.666: E/AndroidRuntime(14088):    at com.android.internal.app.AlertController$ButtonHandler
    .handleMessage(AlertController.java:166)
09-29 10:06:43.666: E/AndroidRuntime(14088):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 10:06:43.666: E/AndroidRuntime(14088):    at android.os.Looper.loop(Looper.java:137)
09-29 10:06:43.666: E/AndroidRuntime(14088):    at android.app.ActivityThread.main(ActivityThread.java:4424)
09-29 10:06:43.666: E/AndroidRuntime(14088):    at java.lang.reflect.Method.invokeNative(Native Method)
09-29 10:06:43.666: E/AndroidRuntime(14088):    at java.lang.reflect.Method.invoke(Method.java:511)
09-29 10:06:43.666: E/AndroidRuntime(14088):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-29 10:06:43.666: E/AndroidRuntime(14088):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-29 10:06:43.666: E/AndroidRuntime(14088):    at dalvik.system.NativeStart.main(Native Method)

我究竟做错了什么?如何将选定的值从 DialogFragment 传递到 Fragment?

4

2 回答 2

1

你只需要创建一个接口,你的 Fragment 就应该实现它。

public interface OnTimeDialogListener {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute);       
}

这是代码

public class Fragment_Picker_Time extends DialogFragment implements TimePickerDialog.OnTimeSetListener {


 public interface OnTimeDialogListener {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute);       
 }

 int hour;
 int minute;

 OnTimeDialogListener mListener;    

 @Override
 public void onAttach(Activity activity) {
    super.onAttach(activity);    
    try {    
        mListener = (OnTimeDialogListener) activity;
    } catch (ClassCastException e) {    
        throw new ClassCastException(activity.toString()
                + " must implement OnTimeDialogListener");
    }
 }

 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {
  // Use the current time as the default values for the picker
  final Calendar c = Calendar.getInstance();
  hour = c.get(Calendar.HOUR_OF_DAY);
  minute = c.get(Calendar.MINUTE);

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

 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
      mListener.onTimeSet(TimePicker view, int hourOfDay, int minute);
 }
 }
于 2013-09-29T10:50:35.950 回答
1

尝试用 mListener 换行:

mListener=(...Listener)activity.getFragmentManager().findFragmentById(int RESOURCE);

其中最后一件事是您的片段在主要活动中的 id ......例如,R.id.planList。括号中的第一件事是您的侦听器的名称,例如,NewPlanNameListener

比它确实会将您的点击定向到片段,而不是活动

于 2014-03-19T17:47:40.877 回答