无法将事件插入到Android日历中

时间:2012-11-02 19:34:51

标签: java android calendar android-2.2-froyo

我正在尝试使用以下代码将事件插入日历:

package come.jobfinder.dev;

import java.util.Calendar;

import org.junit.Test;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;

public class CalendarWrapper extends Activity {

    public void syncDataWithCalendar(String workPlace , Calendar date){

        //TODO should be a field that user should fill - intreviewLength
        int intreviewLength = 60;// set it default to be an hour , will be a field user should fill
        //TODO should be a field that will be sent - position
        String position = "Product Manager";
        //TODO  should be a field that will be sent - location
        String location = "Tel Aviv";
        //TODO need to check where to take this argument from
        long calID = 3;
        Calendar beginTime = Calendar.getInstance();
        beginTime.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.HOUR), date.get(Calendar.MINUTE));
        Calendar endTime = Calendar.getInstance();
        endTime.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.HOUR), date.get(Calendar.MINUTE)+intreviewLength);
        Intent intent = new Intent(Intent.ACTION_INSERT)
                .setData(Events.CONTENT_URI)
                .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
                .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
                .putExtra(Events.TITLE, "Job Interview with "+workPlace)
                .putExtra(Events.DESCRIPTION, "Job interview with "+workPlace+" for the position"+position)
                .putExtra(Events.EVENT_LOCATION, location)
                .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
                .putExtra(Intent.EXTRA_EMAIL, "rowan@example.com,trevor@example.com");

        startActivity(intent);

        //return intent;

        // get the event ID that is the last element in the Uri
    }
}

并收到以下错误:

11-02 21:22:07.580: E/AndroidRuntime(15871): FATAL EXCEPTION: main
11-02 21:22:07.580: E/AndroidRuntime(15871): java.lang.NullPointerException
11-02 21:22:07.580: E/AndroidRuntime(15871):    at android.app.Activity.startActivityForResult(Activity.java:3195)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at android.app.Activity.startActivity(Activity.java:3302)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at come.jobfinder.dev.CalendarWrapper.syncDataWithCalendar(CalendarWrapper.java:45)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at come.jobfinder.dev.UpdateWorkPlace.onClick(UpdateWorkPlace.java:90)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at android.view.View.performClick(View.java:3627)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at android.view.View$PerformClick.run(View.java:14329)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at android.os.Handler.handleCallback(Handler.java:605)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at android.os.Looper.loop(Looper.java:137)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at android.app.ActivityThread.main(ActivityThread.java:4511)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at java.lang.reflect.Method.invokeNative(Native Method)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at java.lang.reflect.Method.invoke(Method.java:511)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
11-02 21:22:07.580: E/AndroidRuntime(15871):    at dalvik.system.NativeStart.main(Native Method)

感谢一位负责人

尼尔

1 个答案:

答案 0 :(得分:0)

很难说出发生了什么。你在跟踪中得到一个空指针异常,哪个源语句失败了?