2016-11-20 26 views
0

我創建我的MainActivity類,啓動一個啓動TimePickerDialog的片段。我想在我的AJOUTPATROUILLEFRAGMENT類的OnAttach函數中檢索時間,但是我找不到一個錯誤。如果你可以看。錯誤TimePickerDialog。這裏是代碼:

 
ublic final class AJOUTPATROUILLEFRAGMENT extends Fragment implements DATEPICKERDIALOGCLASS.OnCompleteListener , TIMEPICKERDIALOGCLASS.OnCompleteListener{ 


    View myView; 
    Button BTQUITTER; 
    Button BTAJOUT; 

    Button BTHEUREDEBUTPATROUILLE; 
    static Button BTHEUREFINPATROUILLE; 
    Button BTAJOUTAGENTPATROUILLE; 

    FragmentManager fragmentManager; 
    String DATEHEURE = new String(); 
    DATEPICKERDIALOGCLASS DateInfo; 


    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     myView = inflater.inflate(R.layout.ajout_patrouille_layout,container,false); 


     BTAJOUT    = (Button) myView.findViewById(R.id.btnAjoutPatrouille); 
     BTQUITTER    = (Button) myView.findViewById(R.id.btnQuitterPatrouille); 
     BTHEUREDEBUTPATROUILLE = (Button) myView.findViewById(R.id.btnHeureDebutPatrouille); 
     BTHEUREFINPATROUILLE = (Button) myView.findViewById(R.id.btnHeureFinPatrouille);; 
     BTAJOUTAGENTPATROUILLE = (Button) myView.findViewById(R.id.btnAjoutAgentPatrouille);; 





     BTAJOUT.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 


      } 
     }); 

     BTQUITTER.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 

       fragmentManager = getFragmentManager(); 
       fragmentManager.beginTransaction().replace(R.id.content_frame, new PATROUILLEFRAGMENT()).commit(); 

      } 
     }); 

     BTHEUREDEBUTPATROUILLE.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 

       TIMEPICKERDIALOGCLASS TimeInfo = new TIMEPICKERDIALOGCLASS(); 
TimeInfo.show(getFragmentManager(),"TIMEPICKER"); 

      } 
     }); 

     BTHEUREFINPATROUILLE.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 

      } 
     }); 

     BTAJOUTAGENTPATROUILLE.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 


      } 
     }); 

     return myView; 
    } 
    public void onCompleteDate(String date) { 
     // After the dialog fragment completes, it calls this callback. 
     // use the string here 


    } 

    public void onCompleteTime(String time) 
    { 

    } 

} 

這裏是DialogFragment

 

public class TIMEPICKERDIALOGCLASS extends DialogFragment implements TimePickerDialog.OnTimeSetListener { 

    private OnCompleteListener mListener; 


    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 


     final Calendar c = Calendar.getInstance(); 
     int hour = c.get(Calendar.HOUR_OF_DAY); 
     int minute = c.get(Calendar.MINUTE); 


     return new TimePickerDialog(getActivity(), this, hour, minute, 
       DateFormat.is24HourFormat(getActivity())); 
    } 

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 


     String time =Integer.toString(hourOfDay)+" : " +Integer.toString(minute); 
     this.mListener.onCompleteTime(time); 

    } 


    public interface OnCompleteListener { 
     public abstract void onCompleteTime(String time); 
    } 



    public void onAttach(Activity activity) { 

     super.onAttach(activity); 

     try { 
      this.mListener = (OnCompleteListener)activity; 
     } 
     catch (final ClassCastException e) { 
      throw new ClassCastException(activity.toString() + " must implement OnCompleteListener"); 
     } 
    } 

} 

 

D/ViewRootImpl: ViewPostImeInputStage processPointer 1 
D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.demo.beos.policev12, PID: 1914 
        java.lang.ClassCastException: [email protected] must implement OnCompleteListener 
         at com.demo.beos.policev12.DIALOG.TIMEPICKERDIALOGCLASS.onAttach(TIMEPICKERDIALOGCLASS.java:58) 
         at android.support.v4.app.Fragment.onAttach(Fragment.java:1165) 
         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019) 
         at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:779) 
         at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:802) 
         at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:660) 
         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
         at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
         at android.os.Handler.handleCallback(Handler.java:739) 
         at android.os.Handler.dispatchMessage(Handler.java:95) 
         at android.os.Looper.loop(Looper.java:158) 
         at android.app.ActivityThread.main(ActivityThread.java:7229) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

回答

0

我TimePickerDialog類應實現OnCompleteListener在MainActivity不AJOUTPATROUILLEFRAGMENT,

從主要活動,你可以將消息傳遞給片段。 ex:

public class MainActivity extends AppCompatActivity implements TIMEPICKERDIALOGCLASS.OnCompleteListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // set layout and fragment. 

    } 

    public void onCompleteTime(String time) { 

     TIMEPICKERDIALOGCLASS fragment = null;// find by id or tag 

     fragment.onCompleteTime(time); 
    } 
} 
+0

ok謝謝,我做了改變,它確定。這裏是代碼:AJOUTPATROUILLEFRAGMENT Test =(AJOUTPATROUILLEFRAGMENT)getSupportFragmentManager()。findFragmentById(R.id.content_frame); Test.onCompleteTime(time); –

+0

好吧它的工作很好.. ..? –