在Android中添加日历事件 - Android

时间:2015-03-10 20:46:22

标签: android android-activity android-5.0-lollipop android-4.4-kitkat

我是Android应用程序开发的新手,我正在尝试开发一个应用程序,它将在我的Android手机中添加一个事件。这是我试过的代码:

问题:我的应用程序崩溃了。

AddCalendarEventTestAction.java

package com.app.mytestapp;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.CalendarContract;
import android.util.Log;

import java.util.Calendar;
import java.util.HashMap;
import java.util.TimeZone;
public class AddCalendarEventTestAction {
     String[] CALENDAR_QUERY_COLUMNS = { "_id", "name", "visible", "ownerAccount" };
    CalendarContract contract;
    String eventStartTitle = "POST Test";
    int eventStartYear;
    Context contx;

    public AddCalendarEventTestAction()
    {
    }


    private void addEvent()
    {
        Calendar localCalendar1 = Calendar.getInstance();
        localCalendar1.set(2015, 3, 4, 1, 40);
       // localCalendar1.set(this.eventStartYear, this.eventStartMonth, this.eventStartDate, this.eventStartHour, this.eventStartMinute);
        Calendar localCalendar2 = Calendar.getInstance();
        localCalendar2.set(2015, 3, 4, 3, 45);
        ContentResolver localContentResolver = contx.getContentResolver();
       Log.d("Test",CalendarContract.Calendars.CONTENT_URI.toString());
        Cursor localCursor = localContentResolver.query(CalendarContract.Calendars.CONTENT_URI, CALENDAR_QUERY_COLUMNS, null, null, null);
        Log.d("Test","Calendar cursor = " + localCursor);
        if (localCursor.moveToNext())
        {
            localCursor.getString(0);
            localCursor.getString(1);
            if (!localCursor.getString(2).equals("0"));
            for (boolean bool = true; ; bool = false)
            {
                Boolean.valueOf(bool);
                String str = localCursor.getString(3);
              Log.d("Test","Found calendar " + str);
                break;
            }
        }
        TimeZone timeZone = TimeZone.getDefault();
        ContentValues localContentValues = new ContentValues();
        localContentValues.put(CalendarContract.Events.CALENDAR_ID, Integer.valueOf(1));
        localContentValues.put(CalendarContract.Events.TITLE, this.eventStartTitle);
        localContentValues.put(CalendarContract.Events.DTSTART, localCalendar1.getTimeInMillis());
        localContentValues.put(CalendarContract.Events.EXDATE, localCalendar2.getTimeInMillis());
        localContentValues.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
        Integer.parseInt(localContentResolver.insert(CalendarContract.Events.CONTENT_URI, localContentValues).getLastPathSegment());
    }


    public void start()
    {
        addEvent();
    }
}

MainActivity.java

package com.app.mytestapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.telephony.SmsManager;
import android.view.View;

import java.util.ArrayList;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

      public void performAction(View view)
      {
          AddCalendarEventTestAction calobj = new AddCalendarEventTestAction();
          calobj.start();

      }

}

Menifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.mytestapp" >



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.SEND_SMS">
    </uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS">
    </uses-permission>
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />

</manifest>

Logcat:

java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3823)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18439)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5034)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18439)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5034)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.app.mytestapp.AddCalendarEventTestAction.addEvent(AddCalendarEventTestAction.java:38)
            at com.app.mytestapp.AddCalendarEventTestAction.start(AddCalendarEventTestAction.java:68)
            at com.app.mytestapp.MainActivity.performAction(MainActivity.java:48)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18439)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5034)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method

1 个答案:

答案 0 :(得分:0)

找到解决方案:我没有初始化上下文,这就是我的应用程序崩溃的原因。刚刚添加下面的代码

Context mycontx;

    public AddCalendarEventTestAction(Context contx)
    {
        mycontx =contx;
    }