我有一个带有 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" />
现在我已经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);
}
这是抛出 anIllegalStateException: Could not execute method of the activity
并且应用程序强制关闭的最后一行。我想我没有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)