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