编程语言
首页 > 编程语言> > 使用Java添加Android日历事件

使用Java添加Android日历事件

作者:互联网

嗨,我正在尝试将事件添加到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>

Logcat

我是android开发的新手.我不完全了解logcat.我在命令提示符下运行了logcat commmand,得到了这个

    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

解决方法:

您的代码有误:

> new String [] {“ calendar_id”,“ displayname”},null,null,null);

日历数据库中没有名称为calendar_id的列.正确的是_id而不是calendar_id.因此它将是新的String [] {“ __ id”,“ displayname”},null,null,null);

并参阅此参考. https://stackoverflow.com/a/10310258http://android.arnodenhond.com/tutorials/calendar

编辑

要插入新事件,需要3个值. Calendar_id,EventStartTime和EventEndTime.没有这些值,您将无法插入新事件. EventStartTime和EventEndTime需要为纪元格式.

对于正常日期到纪元日期: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);

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

谢谢…

标签:android-calendar,java,android
来源: https://codeday.me/bug/20191201/2080526.html