您好我正在尝试将事件添加到 android 日历。我是android开发的新手。请帮助我如何将事件添加到 android 日历。这是我的代码,它不起作用。当我运行它时,它会给我一条消息“应用程序日历(进程 com.something.something)已意外停止”并有此按钮“强制停止”。这是我的代码
package com.zafar.calendar;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
public class Calendar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), new String[]{"_id", "displayname"}, null, null, null);
cursor.moveToFirst();
// Get calendars name
String calendarNames[] = new String[cursor.getCount()];
// Get calendars id
int[] calendarId = new int[cursor.getCount()];
for (int i = 0; i < calendarNames.length; i++)
{
calendarId[i] = cursor.getInt(0);
calendarNames[i] = cursor.getString(1);
cursor.moveToNext();
}
cursor.close();
ContentValues contentEvent = new ContentValues();
contentEvent.put("calendar_id", 1);
contentEvent.put("title", "Wedding");
contentEvent.put("eventLocation", "New York");
contentEvent.put("dtstart","1335432431000");
contentEvent.put("dtend","1335436031000");
Uri eventsUri = Uri.parse("content://com.android.calendar/events");
getContentResolver().insert(eventsUri, contentEvent);
}
}
更新 这是我的清单文件
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Calendar"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission>
</application>
日志猫
我是android开发的新手。我不完全了解 logcat。我在命令提示符下运行了 logcat 命令,我得到了这个
04-28 09:16:57.956: D/AndroidRuntime(333): Shutting down VM
04-28 09:16:57.956: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-28 09:16:57.975: E/AndroidRuntime(333): FATAL EXCEPTION: main
04-28 09:16:57.975: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zafar.calendar/com.zafar.calendar.Calendar}: java.lang.NullPointerException
04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-28 09:16:57.975: E/AndroidRuntime(333): at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 09:16:57.975: E/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123)
04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-28 09:16:57.975: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 09:16:57.975: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507)
04-28 09:16:57.975: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-28 09:16:57.975: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-28 09:16:57.975: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
04-28 09:16:57.975: E/AndroidRuntime(333): Caused by: java.lang.NullPointerException
04-28 09:16:57.975: E/AndroidRuntime(333): at com.zafar.calendar.Calendar.onCreate(Calendar.java:51)
04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-28 09:16:57.975: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-28 09:16:57.975: E/AndroidRuntime(333): ... 11 more
04-28 09:21:58.095: I/Process(333): Sending signal. PID: 333 SIG: 9