我有一个按钮,其onclick处理程序定义如下:
<Button
android:id="@+id/ImportCalendar"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="34dp"
android:onClick="icaltesting"
android:paddingLeft="10dp"
android:text="Import" />
现在我已经在我的activity类中定义了testing()
函数,我想使用它
getContentResolver()
函数最终在我的Android设备中创建本地日历。
我使用的代码如下:
public void icaltesting(View view) {
Uri calUri = CalendarContract.Calendars.CONTENT_URI;
ContentValues cv = new ContentValues();
cv.put(CalendarContract.Calendars.NAME, "Test");
cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "Local Calendar");
cv.put(CalendarContract.Calendars.VISIBLE, 1);
cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
calUri = calUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "Test")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
.build();
Uri result = getContentResolver().insert(calUri, cv);
}
这是抛出IllegalStateException: Could not execute method of the activity
并且app力关闭的最后一行。我想我没有正确地调用getContentResolver()
函数。我该如何处理这个问题?
修改1 相关的堆栈跟踪是这样的:
08-23 20:05:56.580: E/AndroidRuntime(24197): FATAL EXCEPTION: main
08-23 20:05:56.580: E/AndroidRuntime(24197): java.lang.IllegalStateException: Could not execute method of the activity
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.view.View$1.onClick(View.java:3044)
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.view.View.performClick(View.java:3511)
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.view.View$PerformClick.run(View.java:14105)
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.os.Handler.handleCallback(Handler.java:605)
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.os.Looper.loop(Looper.java:137)
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.app.ActivityThread.main(ActivityThread.java:4575)
08-23 20:05:56.580: E/AndroidRuntime(24197): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 20:05:56.580: E/AndroidRuntime(24197): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 20:05:56.580: E/AndroidRuntime(24197): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-23 20:05:56.580: E/AndroidRuntime(24197): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-23 20:05:56.580: E/AndroidRuntime(24197): at dalvik.system.NativeStart.main(Native Method)
08-23 20:05:56.580: E/AndroidRuntime(24197): Caused by: java.lang.reflect.InvocationTargetException
08-23 20:05:56.580: E/AndroidRuntime(24197): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 20:05:56.580: E/AndroidRuntime(24197): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.view.View$1.onClick(View.java:3039)
08-23 20:05:56.580: E/AndroidRuntime(24197): Caused by: java.lang.IllegalArgumentException: the name must not be empty: null
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:166)
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:415)
08-23 20:05:56.580: E/AndroidRuntime(24197): at android.content.ContentResolver.insert(ContentResolver.java:735)
08-23 20:05:56.580: E/AndroidRuntime(24197): at com.example.myfirstapp.CalendarTest.icaltesting(CalendarTest.java:117)
答案 0 :(得分:0)
我想如果仔细观察堆栈跟踪,你会发现另一个异常。这个例外是根本原因。
但实际上这并不重要。没有正确的方法从onClick处理程序调用getContentResolver()。你根本不应该在UI线程上使用它。改为安排AsyncTask
。
更新:根据文档,您缺少几个必填字段
插入
插入新日历时,必须包含以下字段:
ACCOUNT_NAME
ACCOUNT_TYPE
NAME
CALENDAR_DISPLAY_NAME
CALENDAR_COLOR
CALENDAR_ACCESS_LEVEL
OWNER_ACCOUNT