2012-11-28 48 views
1

我想在日曆中插入一個事件,它在android 4+和android 2.2-中完美工作,但是當我在android 2.3中運行應用程序時,我有一個像這樣的結果:錯誤ACTION_INSERT android 2.3

無活動處理

Intent { act=android.intent.action.INSERT dat=content://com.android.calendar/events (has extras) } 

我在這裏展示的代碼:

int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
      Uri u; 
      if (currentapiVersion > android.os.Build.VERSION_CODES.FROYO){ 
       u = Uri.parse("content://com.android.calendar/events"); 
      } else{ 
       u = Uri.parse("content://calendar/events"); 
      } 

      Intent intent = new Intent(Intent.ACTION_INSERT) 
        .setData(u) 
        .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin.getTimeInMillis()) 
        .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end.getTimeInMillis()) 
        .putExtra(Events.TITLE, title) 
        .putExtra(Events.DESCRIPTION, description) 
        .putExtra(Events.EVENT_LOCATION, getString(R.string.lugar)+ ": "+lugar)      
        .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY); 

        String dest = new String(); 
        for(int k=0;k<destinatarios.length;k++){ 
         if(dest.equals("")){ 
          dest = destinatarios[k]; 
         }else{ 
          dest += ","+destinatarios[k]; 
         } 

        } 
        intent.putExtra(Intent.EXTRA_EMAIL, dest); 

      startActivity(intent); 

我展示的logcat:

11-28 12:13:06.113: E/AndroidRuntime(350): FATAL EXCEPTION: main 
11-28 12:13:06.113: E/AndroidRuntime(350): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSERT dat=content://com.android.calendar/events (has extras) } 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.app.Activity.startActivityForResult(Activity.java:2827) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.app.Activity.startActivity(Activity.java:2933) 
11-28 12:13:06.113: E/AndroidRuntime(350): at com.moya.gestor.futbol.EventList.onContextItemSelected(EventList.java:283) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.app.Activity.onMenuItemSelected(Activity.java:2209) 
11-28 12:13:06.113: E/AndroidRuntime(350): at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2785) 
11-28 12:13:06.113: E/AndroidRuntime(350): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143) 
11-28 12:13:06.113: E/AndroidRuntime(350): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855) 
11-28 12:13:06.113: E/AndroidRuntime(350): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137) 
11-28 12:13:06.113: E/AndroidRuntime(350): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:873) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.widget.ListView.performItemClick(ListView.java:3513) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.os.Handler.handleCallback(Handler.java:587) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.os.Looper.loop(Looper.java:123) 
11-28 12:13:06.113: E/AndroidRuntime(350): at android.app.ActivityThread.main(ActivityThread.java:3683) 
11-28 12:13:06.113: E/AndroidRuntime(350): at java.lang.reflect.Method.invokeNative(Native Method) 
11-28 12:13:06.113: E/AndroidRuntime(350): at java.lang.reflect.Method.invoke(Method.java:507) 
11-28 12:13:06.113: E/AndroidRuntime(350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
11-28 12:13:06.113: E/AndroidRuntime(350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
11-28 12:13:06.113: E/AndroidRuntime(350): at dalvik.system.NativeStart.main(Native Method) 

謝謝。

回答

1

試試這個方法來獲取事件的URI不同的操作系統版本

@TargetApi(14) 
private String getCalendarUriBase(Activity act) { 

    String calendarUriBase = null; 
    Uri calendars = Uri.parse("content://calendar/calendars"); 
    Cursor managedCursor = null; 
    try { 
     managedCursor = act.managedQuery(calendars, null, null, null, null); 
    } catch (Exception e) { 
    } 
    if (managedCursor != null) { 
     calendarUriBase = "content://calendar/"; 
    } else { 
     calendars = Uri.parse("content://com.android.calendar/calendars"); 
     try { 
      managedCursor = act.managedQuery(calendars, null, null, null, null); 
     } catch (Exception e) { 
     } 
     if (managedCursor != null) { 
      calendarUriBase = "content://com.android.calendar/"; 
     } 
    } 
    return calendarUriBase; 
} 

的URI獲得不同的版本,你可以使用字段名特定版本不同。 CalendarContract可從4.0版本獲得

ContentResolver cr = getContentResolver(); 
ContentValues values = new ContentValues(); 
Uri EVENTS_URI = null; 
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){ 
     values.put(CalendarContract.Events.DTSTART, starttime); 
     values.put(CalendarContract.Events.DTEND, endtime); 
     values.put(CalendarContract.Events.TITLE, eventName); 
     values.put(CalendarContract.Events.EVENT_LOCATION, eventLocation); 
     values.put(CalendarContract.Events.DESCRIPTION, eventName); 
     values.put(CalendarContract.Events.CALENDAR_ID, 1); 
     values.put(CalendarContract.Events.HAS_ALARM,1); 
     values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID()); 
     values.put(CalendarContract.Events.ALL_DAY, 0); 
     values.put(CalendarContract.Events.HAS_ALARM, 1); 
     values.put(CalendarContract.Events.ACCESS_LEVEL, CalendarContract.Events.ACCESS_DEFAULT); 

     EVENTS_URI = CalendarContract.Events.CONTENT_URI; 
    } 
    else{ 
     EVENTS_URI = Uri.parse(getCalendarUriBase(getInstance()) + "events"); 

     values.put("calendar_id", 1); 
     values.put("title", eventName); 
     values.put("allDay", 0); 
     values.put("dtstart", starttime); 
     values.put("dtend", endtime); 
     values.put("description", eventName); 
     values.put("visibility", 0); 
     values.put("hasAlarm", 1); 
     values.put("eventLocation", eventLocation); 
     values.put("eventTimezone", TimeZone.getDefault().getID()); 
    } 
//  Log.e("check", EVENTS_URI.toString()); 

cr.insert(EVENTS_URI, values);