我想在
Android中添加一个未同步的新日历.我编写了一个使用API级别14的新CalendarContract API执行此操作的应用程序.以下代码工作正常.我可以在Android的日历应用中看到新日历,并为其添加活动.
问题是当我选择时,Android日历应用程序崩溃:菜单 – >要显示的日历 – >要同步的日历
我的代码中有什么问题或是Android日历中的错误吗?
请注意,我不想实际使用同步适配器. CalendarContract.Calendars的文档说不需要这样做:“如果需要本地日历,应用程序可以通过插入同步适配器并使用ACCOUNT_TYPE ACCOUNT_TYPE_LOCAL来实现.”我知道“作为同步适配器插入”意味着将(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)