我读了上一个问题Here,但它并没有完全回答我的问题。 我正在尝试做一些简单的操作,如按下按钮并输入事件,但程序继续崩溃。我的代码是:
package com.example.events;
import java.util.Calendar;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.provider.CalendarContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.events.R;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Perform action on click
addCalendarEvent();
}
});
}
public void addCalendarEvent() {
long calID = 1;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2015, 8, 9, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2015, 8, 9, 8, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Jazzercise");
values.put(CalendarContract.Events.DESCRIPTION, "Group workout");
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, "America/New_York");
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
}
}
我的AndroidManifest.xml看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.events" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<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>
所以,基本上我要做的就是按下该按钮并在日历中添加一个事件,而不提示用户输入任何细节。任何帮助,将不胜感激。 这是错误的堆栈跟踪:
08-29 18:14:44.108 13218-13218/com.example.events E/dalvikvm﹕ >>>>> Normal User
08-29 18:14:44.118 13218-13218/com.example.events E/dalvikvm﹕ >>>>> com.example.events [ userId:0 | appId:10255 ]
08-29 18:14:52.106 13218-13218/com.example.events E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.events, PID: 13218
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.calendar.CalendarProvider2 from ProcessRecord{44f66d78 13218:com.example.events/u0a255} (pid=13218, uid=10255) requires android.permission.READ_CALENDAR or android.permission.WRITE_CALENDAR
at android.os.Parcel.readException(Parcel.java:1465)
at android.os.Parcel.readException(Parcel.java:1419)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3299)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4843)
at android.app.ContextImpl$ApplicationContentResolver.acquireProvider(ContextImpl.java:2718)
at android.content.ContentResolver.acquireProvider(ContentResolver.java:1390)
at android.content.ContentResolver.insert(ContentResolver.java:1196)
at com.example.events.MainActivity.addCalendarEvent(MainActivity.java:60)
at com.example.events.MainActivity$1.onClick(MainActivity.java:33)
at android.view.View.performClick(View.java:4753)
at android.view.View$PerformClick.run(View.java:19562)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您的堆栈跟踪表明您缺少权限。您需要将堆栈跟踪中提到的权限添加到Android清单中。请参阅:How to add manifest permission to android application?