且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java.lang.IllegalArgumentException异常:坏类:类java.lang.String?

更新时间:2023-01-24 12:36:41

阅读的日历提供商了解访问日历事件。

您所做的错误是:

Please I get this error that I do not understand.

This is the code:

Cursor cursor = context.getContentResolver()
            .query(Uri.parse("content://com.android.calendar/events"),
                    new String[] { "calendar_id", "title", "description",
                            "dtstart", "dtend", "eventLocation","allDay" }, null,
                    null, null);
    cursor.moveToFirst();
    // fetching calendars name
    String CNames[] = new String[cursor.getCount()];

    for (int i = 0; i < CNames.length; i++) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        String day =df.format(cursor.getString(3));

This is the LogCat:

08-30 10:07:09.560: E/AndroidRuntime(5008): FATAL EXCEPTION: main
08-30 10:07:09.560: E/AndroidRuntime(5008): java.lang.IllegalArgumentException: Bad     class: class java.lang.String
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     java.text.DateFormat.format(DateFormat.java:296)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     java.text.Format.format(Format.java:93)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     com.examples.android.calendar.Utility.readCalendarEvent(Utility.java:46)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     com.examples.android.calendar.CalendarView$1.run(CalendarView.java:185)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     android.os.Handler.handleCallback(Handler.java:730)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     android.os.Handler.dispatchMessage(Handler.java:92)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     android.os.Looper.loop(Looper.java:137)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     android.app.ActivityThread.main(ActivityThread.java:5103)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     java.lang.reflect.Method.invokeNative(Native Method)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     java.lang.reflect.Method.invoke(Method.java:525)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-30 10:07:09.560: E/AndroidRuntime(5008):     at     dalvik.system.NativeStart.main(Native Method)

Read the API Guide on Calendar Provider to know about accessing the calendar events.

The mistakes you are making are:

  • Do not use hard coded string literals ("dtstart", ..); use the constants defined in CalendarContract.EventsColumns for the column names.
  • The DTSTART and DTEND columns contain the start and end time in long format (milliseconds since epoch).