3

您好我正在尝试将事件添加到 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
4

2 回答 2

3

您的代码中的错误:

  1. new String[]{"calendar_id", "displayname"}, null, null, null);

日历数据库中不存在名称 calendar_id 的列。正确的是_id而不是 calendar_id。所以它将是 new String[]{" _id ", "displayname"}, null, null, null);

并看到这个参考。https://stackoverflow.com/a/10310258http://android.arnodenhond.com/tutorials/calendar

编辑

要插入新事件,需要 3 个值。Calendar_id、EventStartTime 和 EventEndTime。如果没有这些值,您将无法插入新事件。EventStartTime 和 EventEndTime 需要转换为 epoch 格式。

对于正常日期到纪元日期:https
://stackoverflow.com/a/6687502/1160207 对于从纪元到正常日期:https ://stackoverflow.com/a/10028980/1160207

这是对我来说完美的代码。

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);

希望这会帮助你。如果它对您有帮助,请不要忘记接受它。

谢谢...

于 2012-04-28T08:52:21.143 回答
0

如果您希望集成特定的日历应用程序,请联系他们的开发人员并确定他们是否提供 API。因此,例如,Mayra 引用的 Android 开源项目中的日历应用程序没有提供文档化和支持的 API。谷歌甚至明确告诉开发人员不要使用 Mayra 引用的教程中概述的技术。

另一种选择是让您将事件添加到相关的 Internet 日历中。例如,将事件从 Android 开源项目添加到日历应用程序的最佳方法是通过适当的 GData API 将事件添加到用户的 Google 日历。

于 2012-04-29T02:56:45.047 回答