如何在调用Fragment中从DialogFragment获取结果?

时间:2013-09-29 10:23:06

标签: android android-fragments datetimepicker android-dialogfragment

我有以下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?

2 个答案:

答案 0 :(得分: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);
 }
 }

答案 1 :(得分:1)

尝试使用mListener更改行:

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

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

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