0
我需要能夠從我的Android應用程序在Google日曆中創建一個事件。我相信有一個日曆API,但我從來沒有使用它。我對Android開發相當陌生,所以我從早先的瀏覽中找到了一些示例,並使用以下代碼嘗試更新我的Android日曆。如何從我的Android代碼動態更新日曆
public static boolean updateCalendar(Context context,String cal_Id,String eventId)
{
try{
Uri CALENDAR_URI = Uri.parse(CAL_URI+"events");
Cursor c = context.getContentResolver().query(CALENDAR_URI, null, null, null, null);
String[] s = c.getColumnNames();
if (c.moveToFirst())
{
while (c.moveToNext())
{
String _id = c.getString(c.getColumnIndex("_id"));
String CalId = c.getString(c.getColumnIndex("calendar_id"));
if ((_id==null) && (CalId == null))
{
return false;
}
else
{
if (_id.equals(eventId) && CalId.equals(cal_Id))
{
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(_id));
context.getContentResolver().update(uri, null, null, null);// need to give your data here
return true;
}
}
}
}
}
finally
{
return true;
}
}
然而,當我運行它getColumnNames不會被調用,代碼跳轉直接到線context.getContentResolver()更新(URI,NULL,NULL,NULL);然後退出。
我把一對夫婦的測試賽在我的日曆,爲什麼這些代碼並沒有接他們?
感謝您的回覆,我已經嵌入代碼到我的應用程序,但我發現一對夫婦的錯誤消息。這兩行代碼Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this)+「events」); ContentResolver cr = getContentResolver();正在拋出錯誤消息,第一個說'你不能在靜態上下文中使用它',第二個說'不能從ContextWrapper類型對非靜態方法getContentResolver()進行靜態引用「。 – user616076
可能已放置在靜態方法的代碼檢查一次,可能不會在模擬器 – Abhi
http://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-工作方法檢查這個 – Abhi