java-无法转换为android.app.DatePickerDialog $OnDateSetListener

java-无法转换为android.app.DatePickerDialog $OnDateSetListener,第1张

概述在应用程序中单击按钮时,我意外地遇到了以下错误:cannotbecasttoandroid.app.DatePickerDialog$OnDateSetListener发生的情况如下-用户单击名为“btnCalendar”的按钮,然后显示日历btnCalendar.setOnClickListener(newOnClickListener(){@Overridepublicvo

在应用程序中单击按钮时,我意外地遇到了以下错误:

 cannot be cast to androID.app.DatePickerDialog$OnDateSetListener

发生的情况如下-用户单击名为“ btnCalendar”的按钮,然后显示日历

btnCalendar.setonClickListener(new OnClickListener() {    @OverrIDe    public voID onClick(VIEw arg0) {        DialogP dcal = new  DialogP();       dcal.show(ScheduleMatchOptionActivity.this.getFragmentManager(),"MyDialog");    }});

下面是DialogP类

public class DialogP extends DialogFragment {    public Dialog onCreateDialog(Bundle savedInstanceState) {        // Use the current date as the default date in the picker        final Calendar c = Calendar.getInstance();        int year = c.get(Calendar.YEAR);        int month = c.get(Calendar.MONTH);        int day = c.get(Calendar.DAY_OF_MONTH);         //  Create a new instance of DatePickerDialog and return it        DatePickerDialog dialog = new DatePickerDialog(getActivity(), (OnDateSetListener) this, year, month,day);        Log.i("onCreateDialog", "onCreateDialog : MinDate() : "+new Date().getTime());        Calendar cal=new GregorianCalendar();        //min date        dialog.getDatePicker().setMinDate(cal.getTime().getTime()-1000);        Log.i("onCreateDialog", "onCreateDialog : MinDate() : "+cal.getTime());        //max date .... added 30 days from currunt date        cal.add(Calendar.DATE, 30);        Date dte =cal.getTime();        dialog.getDatePicker().setMaxDate(dte.getTime());        Log.i("onCreateDialog", "onCreateDialog : MaxDate() : "+dte);        //customizing calendar vIEw        dialog.getDatePicker().setCalendarVIEwShown(true);        dialog.getDatePicker().setSpinneRSShown(false);        dialog.getDatePicker().getCalendarVIEw().setShowWeekNumber(false);        dialog.setCancelable(true);        Log.i("Date", "date : "+System.currentTimeMillis());        return dialog;    }}

以下是日志猫消息

 09-30 16:12:00.805: E/AndroIDRuntime(1415): FATAL EXCEPTION: main    09-30 16:12:00.805: E/AndroIDRuntime(1415): java.lang.classCastException: com.dooba.beta.DialogP cannot be cast to androID.app.DatePickerDialog$OnDateSetListener    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at com.dooba.beta.DialogP.onCreateDialog(DialogP.java:25)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at androID.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at androID.app.FragmentManagerImpl.movetoState(FragmentManager.java:885)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at androID.app.FragmentManagerImpl.movetoState(FragmentManager.java:1057)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at androID.app.BackStackRecord.run(BackStackRecord.java:682)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at androID.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at androID.app.FragmentManagerImpl.run(FragmentManager.java:441)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at androID.os.Handler.handleCallback(Handler.java:730)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at androID.os.Handler.dispatchMessage(Handler.java:92)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at androID.os.Looper.loop(Looper.java:137)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at androID.app.ActivityThread.main(ActivityThread.java:5103)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at java.lang.reflect.Method.invokeNative(Native Method)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at java.lang.reflect.Method.invoke(Method.java:525)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:553)    09-30 16:12:00.805: E/AndroIDRuntime(1415):     at dalvik.system.NativeStart.main(Native Method)

第25行如下:

DatePickerDialog dialog = new DatePickerDialog(getActivity(), (OnDateSetListener) this, year, month,day);

在我去之前

 DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month,day); 

但收到以下错误“未定义构造函数DatePickerDialog(Activity,DialogP,int,int,int)”,因此我将其强制转换.

我想检索另一个日期相关的问题,并将日期转换为显示给用户的名为txtDate的文本视图.我对settext很熟悉,但有点困惑.

任何帮助将不胜感激.提前致谢

更新:

cal.add(Calendar.DATE,7);
如果数字小于12,则发生以下错误:

ALL i want to do is to limit the calendar to one week    09-30 16:39:07.217: E/AndroIDRuntime(2410): FATAL EXCEPTION: main    09-30 16:39:07.217: E/AndroIDRuntime(2410): java.lang.NullPointerException    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.CalendarVIEw.onScroll(CalendarVIEw.java:1214)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.CalendarVIEw.access0(CalendarVIEw.java:76)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.CalendarVIEw.onScroll(CalendarVIEw.java:1075)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.AbsListVIEw.invokeOnItemScrollListener(AbsListVIEw.java:1340)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.ListVIEw.layoutChildren(ListVIEw.java:1762)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.AbsListVIEw.onLayout(AbsListVIEw.java:2012)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1671)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.linearLayout.layoutVertical(linearLayout.java:1525)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.FrameLayout.onLayout(FrameLayout.java:448)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4562)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1671)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.linearLayout.layoutHorizontal(linearLayout.java:1660)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.linearLayout.onLayout(linearLayout.java:1436)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4562)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.FrameLayout.onLayout(FrameLayout.java:448)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4562)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.FrameLayout.onLayout(FrameLayout.java:448)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4562)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.FrameLayout.onLayout(FrameLayout.java:448)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4562)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1671)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.linearLayout.layoutVertical(linearLayout.java:1525)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.linearLayout.onLayout(linearLayout.java:1434)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4562)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.FrameLayout.onLayout(FrameLayout.java:448)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4562)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.FrameLayout.onLayout(FrameLayout.java:448)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4562)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.Widget.FrameLayout.onLayout(FrameLayout.java:448)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEw.layout(VIEw.java:14289)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwGroup.layout(VIEwGroup.java:4562)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwRootImpl.performTraversals(VIEwRootImpl.java:1730)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwRootImpl.doTraversal(VIEwRootImpl.java:1004)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.VIEwRootImpl$TraversalRunnable.run(VIEwRootImpl.java:5481)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.Choreographer$CallbackRecord.run(Choreographer.java:749)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.Choreographer.doCallbacks(Choreographer.java:562)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.Choreographer.doFrame(Choreographer.java:532)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.vIEw.Choreographer$FramedisplayEventReceiver.run(Choreographer.java:735)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.os.Handler.dispatchMessage(Handler.java:92)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.os.Looper.loop(Looper.java:137)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at androID.app.ActivityThread.main(ActivityThread.java:5103)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at java.lang.reflect.Method.invokeNative(Native Method)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at java.lang.reflect.Method.invoke(Method.java:525)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:553)    09-30 16:39:07.217: E/AndroIDRuntime(2410):     at dalvik.system.NativeStart.main(Native Method)

解决方法:

确保在ScheduleMatchOptionActivity活动中实现OnDateSetListener:

public class ScheduleMatchOptionActivity extends FragmentActivity           implements DatePickerDialog.OnDateSetListener {....you code here..}

而不是将getActivity()强制转换为OnDateSetListener,而是将其强制转换为ScheduleMatchOptionActivity,因为您正在ScheduleMatchOptionActivity中实现OnDateSetListener接口:

DatePickerDialog dialog = new DatePickerDialog(getActivity(),                     (ScheduleMatchOptionActivity)getActivity(), year, month,day);
总结

以上是内存溢出为你收集整理的java-无法转换为android.app.DatePickerDialog $OnDateSetListener全部内容,希望文章能够帮你解决java-无法转换为android.app.DatePickerDialog $OnDateSetListener所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1077816.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存