错误TimePickerDialog。这是代码:

时间:2016-11-20 23:00:03

标签: android timepicker

我创建了我的MainActivity类,它启动了一个启动TimePickerDialog的Fragment。我想在我的AJOUTPATROUILLEFRAGMENT类的OnAttach函数中检索时间,但是有一个我找不到的错误。如果你能看到。

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

中的TimePickerDialog类

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: com.demo.beos.policev12.MainActivity@da4d2f4 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)

1 个答案:

答案 0 :(得分:2)

你应该在MainActivity中实现OnCompleteListener,而不是在AJOUTPATROUILLEFRAGMENT中实现,

从主要活动中,您可以将邮件传递给Fragment。 例如:

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);
    }
}