2012-03-20 73 views
4

我想向未添加同步的Android添加新日曆。我已經編寫了一個應用程序,它使用來自API級別14的新CalendarContract API執行此操作。以下代碼正常工作。我可以在Android的日曆應用程序中看到新的日曆並向其添加事件。將包含ACCOUNT_TYPE_LOCAL的日曆添加到Android 4會導致其日曆崩潰

的問題是,Android的日曆應用程序崩潰時我選擇:菜單 - >要顯示的日曆 - >日曆同步

有什麼錯在我的代碼或者是在Android的日曆中的錯誤?

請注意,我不想實際使用同步適配器。 CalendarContract.Calendars的文檔說這不是必需的:「如果需要本地日曆,應用程序可以通過插入同步適配器並使用ACCOUNT_TYPE_LOCAL的ACCOUNT_TYPE來完成此操作。」我知道「插入作爲同步適配器」意味着將(CalendarContract.CALLER_IS_SYNCADAPTER,「true」)追加到URI。

public class CalendarMapper { 

private static final String ACCOUNT_NAME = "private"; 
private static final String INT_NAME_PREFIX = "priv"; 

private static Uri buildCalUri() { 
    return CalendarContract.Calendars.CONTENT_URI 
      .buildUpon() 
      .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true") 
      .appendQueryParameter(Calendars.ACCOUNT_NAME, ACCOUNT_NAME) 
      .appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL) 
      .build(); 
} 

private static ContentValues buildContentValues(Calendar calendar) { 
    String dispName = calendar.getName(); //Calendar.getName() returns a String 
    String intName = INT_NAME_PREFIX + dispName; 
    final ContentValues cv = new ContentValues(); 
    cv.put(Calendars.ACCOUNT_NAME, ACCOUNT_NAME); 
    cv.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL); 
    cv.put(Calendars.NAME, intName); 
    cv.put(Calendars.CALENDAR_DISPLAY_NAME, dispName); 
    cv.put(Calendars.CALENDAR_COLOR, calendar.getColor()); //Calendar.getColor() returns int 
    cv.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER); 
    cv.put(Calendars.OWNER_ACCOUNT, ACCOUNT_NAME); 
    cv.put(Calendars.VISIBLE, 1); 
    cv.put(Calendars.SYNC_EVENTS, 1); 
    return cv; 
} 

public static void addCalendar(Calendar calendar, ContentResolver cr) { 
    if (calendar == null) 
     throw new IllegalArgumentException(); 

    final ContentValues cv = buildContentValues(calendar); 

    Uri calUri = buildCalUri(); 
    cr.insert(calUri, cv); 
} 

} 

下面是導致日曆應用崩潰異常的堆棧跟蹤:

03-22 07:12:39.437: E/AndroidRuntime(9730): FATAL EXCEPTION: main 
    03-22 07:12:39.437: E/AndroidRuntime(9730): java.lang.NullPointerException 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at com.android.calendar.selectcalendars.SelectSyncedCalendarsMultiAccountAdapter.bindGroupView(SelectSyncedCalendarsMultiAccountAdapter.java:324) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.CursorTreeAdapter.getGroupView(CursorTreeAdapter.java:207) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.AbsListView.obtainView(AbsListView.java:2033) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.ListView.makeAndAddView(ListView.java:1772) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.ListView.fillDown(ListView.java:672) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.ListView.fillFromTop(ListView.java:732) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.ListView.layoutChildren(ListView.java:1611) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.AbsListView.onLayout(AbsListView.java:1863) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.View.layout(View.java:11278) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewGroup.layout(ViewGroup.java:4224) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.View.layout(View.java:11278) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewGroup.layout(ViewGroup.java:4224) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.View.layout(View.java:11278) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewGroup.layout(ViewGroup.java:4224) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.View.layout(View.java:11278) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewGroup.layout(ViewGroup.java:4224) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.View.layout(View.java:11278) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewGroup.layout(ViewGroup.java:4224) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.os.Handler.dispatchMessage(Handler.java:99) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.os.Looper.loop(Looper.java:137) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at android.app.ActivityThread.main(ActivityThread.java:4424) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at java.lang.reflect.Method.invokeNative(Native Method) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at java.lang.reflect.Method.invoke(Method.java:511) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
    03-22 07:12:39.437: E/AndroidRuntime(9730): at dalvik.system.NativeStart.main(Native Method) 

回答