【问题标题】:Android : Error in Add event of Calender : Unkown URLAndroid:添加日历事件时出错:未知 URL
【发布时间】:2014-03-21 12:01:19
【问题描述】:

我在 android 日历中工作。我已经使用 android 应用程序以编程方式在日历中添加事件。 我也参考了这个链接:IllegalArgumentException: Unknown URL content://com.android.calendar/events when inserting an event to the calendar on Android Adding events to native calendar is not working 但在我的代码中不起作用。

我的代码是:

ContentValues contentEvent = new ContentValues();
                // Particular Calendar in which we need to add Event
                contentEvent.put("calendar_id", AlarmId);                                                  
                // Title/Caption of the Event     
                contentEvent.put("title", "Wedding");                                                           
                // Description of the Event
                contentEvent.put("description", "Wedding Party");                                  
                // Venue/Location of the Event
                contentEvent.put("eventLocation", "New York");                                                   
                // Start Date of the Event with Time  
                contentEvent.put("dtstart", l);                                                          
                // End Date of the Event with Time
                contentEvent.put("dtend", l+60*1000);                  
                // All Day Event                                       
                contentEvent.put("allDay", 1);                     
                // Set alarm for this Event                                              
                contentEvent.put("hasAlarm",1);     
                contentEvent.put("eventTimezone", android.text.format.Time.getCurrentTimezone());

                Uri eventsUri = getCalendarURI(false);                  

                // event is added successfully
                getContentResolver().insert(eventsUri, contentEvent);
//                  Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);


public Uri getCalendarURI(boolean eventUri) {
    Uri calendarURI = null;
    if (android.os.Build.VERSION.SDK_INT <= 7) {
        calendarURI = (eventUri) ? Uri.parse("content://calendar/events")
                : Uri.parse("content://calendar/calendars");
    } else {
        calendarURI = (eventUri) ? Uri
                .parse("content://com.android.calendar/events") : Uri
                .parse("content://com.android.calendar/calendars");
    }
    return calendarURI;
}

我的问题是:当我运行我的应用程序时,该错误已生成。那么我该如何解决这个错误呢?

错误是:

java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/

提前谢谢你

【问题讨论】:

    标签: java android calendar


    【解决方案1】:
      Calendar cal = Calendar.getInstance();  
    
        long l = cal.getTimeInMillis();
    
        long cal_Id = 1;
    
        **// Also Here Use Cal_Id = 1 not parse another value** 
    
        ContentResolver CR = getContentResolver();
    
    
         ContentValues calEvent  = new ContentValues();
    
         calEvent.put(CalendarContract.Events.CALENDAR_ID,  cal_Id); // XXX pick)
    
         calEvent.put(CalendarContract.Events.TITLE, " Demo Data");
    
         calEvent.put(CalendarContract.Events.DTSTART,l);
    
         calEvent.put(CalendarContract.Events.DTEND, l+60 * 1000);
    
         calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "Indian/Christmas"); 
    

    //这里使用正确的区域时区并解决此错误

         Uri uri = CR.insert(URL, calEvent);                        
    
         int id = Integer.parseInt(uri.getLastPathSegment());                       
    
         Toast.makeText(this, "Created Calendar Event " + id,
                Toast.LENGTH_SHORT).show();
    

    【讨论】:

      【解决方案2】:

      试试这个:

      Uri calendars = getCalendarURI(true);
      
      
      public Uri getCalendarURI(boolean eventUri) {
          Uri calendarURI = null;
          if (android.os.Build.VERSION.SDK_INT <= 7) {
              calendarURI = (eventUri) ? Uri.parse("content://calendar/events")
                      : Uri.parse("content://calendar/calendars");
          } else {
              calendarURI = (eventUri) ? Uri
                      .parse("content://com.android.calendar/events") : Uri
                      .parse("content://com.android.calendar/calendars");
          }
          return calendarURI;
      }
      

      【讨论】:

      • 当您希望日历 uri 将 false 作为参数 o\w true 用于事件 uri 时
      • 我也检查了两个布尔值,但它不起作用并产生相同的错误。
      • 当我在 getCalendarURI(true) 中设置了 true 值时 java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/calendars/-447611156 这个错误已经生成并且当我在 java.lang.nullpointerexeption 生成时,在 getCalendarURI(true) 中设置了 false 值。
      【解决方案3】:

      你可以试试这个方法:

      Calendar cal = Calendar.getInstance();              
      Intent intent = new Intent(Intent.ACTION_EDIT);
      intent.setType("vnd.android.cursor.item/event");
      intent.putExtra("beginTime", cal.getTimeInMillis());
      intent.putExtra("allDay", true);
      intent.putExtra("rrule", "FREQ=YEARLY");
      intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
      intent.putExtra("title", "A Test Event from android app");
      startActivity(intent);
      

      或者你可以在这个主题中找到:

      How to add calendar events in Android?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-09
        • 2012-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-12
        • 1970-01-01
        相关资源
        最近更新 更多