1

我有以下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獲取值並傳遞給我的片段。我試過如下:

在我的onTimeSet功能設置DialogFragment:

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?

回答

1

你只需要創建一個Inteface,你的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

我試圖實現它在我的片段,但該應用程序崩潰,說我必須在我的主要FragmentActivity中實現它。我確實設法在那裏實現它,但是現在我需要再次將變量傳遞給我的Fragment。有沒有辦法將值直接傳遞給我的片段? – Igal

+0

你可以分享代碼嗎? –

+0

請參閱此鏈接:http://developer.android.com/guide/topics/ui/dialogs.html –

1

嘗試改變符合mListener:

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

其中brakets的最後一件事是在主要活動中的片段的ID .. 。例如R.id.planList。並在括號中的第一件事情就是你的監聽器的名稱,例如,NewPlanNameListener

比它的點擊確實直接到片段,而不是活動

相關問題