当前位置: 首页 > 知识库问答 >
问题:

错误TimePickerDialog。代码如下:

黄涵畅
2023-03-14

我创建了MainActivity类,它启动了一个片段,用于启动TimePickerDialog。我想检索AJOUTPATROUILLEFRAGMENT类中的OnAttach()函数中的时间,但有一个我找不到的错误。请你看着。

public 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个答案

裘丰
2023-03-14

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

从主activity,您可以将消息传递给碎片。例:

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);
    }
}
 类似资料:
  • 错误代码 宏定义 #define  RT_EOK   0   无错误   #define  RT_ERROR   1   一般错误   #define  RT_ETIMEOUT   2   超时错误   #define  RT_EFULL   3   资源已满   #define  RT_EEMPTY   4   资源已空   #define  RT_ENOMEM   5   内存不足   #de

  • 说明:编写一条if语句,验证字符串是否包含字符。 添加一个if语句,检查是否大于零。不要忘记if语句末尾的!如果字符串中确实有一些字符,则打印用户的单词。否则(即:语句),请打印空。您需要多次运行代码,测试空字符串和带字符的字符串。当你确信你的代码可以工作时,继续下一个练习。 我被卡住了,因为我一直遇到以下错误。我做错了什么?

  • 我的服务器上的Https不工作并得到。我尝试了太多的东西,但无法得到任何结果。我的带有Nginx反向代理的HttpSpring启动服务器工作得很好。 下面是我的 /etc/nginx/conf.d/*. conf文件: 我已经检查了我的443端口是否打开并正在监听。 我不知道我哪里做错了任何帮助都将感激不尽

  • Welcome to follow me on GitHub GitHub: https://github.com/JZXiang 中文版文档 TimePickerDialog An Android time picker library. Easy to use. Support five types. Able to set the minimun and maximum millsecond

  • 问题内容: 我努力了: 和: 但总是会显示错误代码500。 问题答案: 根据Express(版本4+)文档,您可以使用: http://expressjs.com/4x/api.html#res.status <= 3.8

  • 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导致删除数据库失败 1010:不能删除数据目录导致删除数据库失败 1011:删除数据库文件失败 1012:不能读取系统表中的记录 1020:记录已被其他用户修改 1021:硬盘剩余空间不足,请加大硬盘可用空间 1022:关键字重复,更改记录失败