1

カレンダーから特定のイベントを削除しようとすると、NULL ポインタ例外が返されます

しかし、すべてのイベントを削除すると正常に動作します

挿入クエリからパスを取得し、ほとんどすべてを試しました

これは私のコードです

私の削除イベント クラス

public class DeleteEvent extends Activity {

    public DeleteEvent() {
        // TODO Auto-generated constructor stub
    }

    public int DeleteCalendarEntry(int entryID) {
        int iNumRowsDeleted = 0;

        Uri eventsUri = Uri.parse( "content://com.android.calendar/events");

        Uri eventUri = ContentUris.withAppendedId(eventsUri, entryID);

        iNumRowsDeleted = getContentResolver().delete(eventUri, null, null);

        return iNumRowsDeleted;


    }
}

/////////////////////////////////////////////// ///

image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    chose_add_view_delete();
    }
});

セルをクリックすると、これが実行されます:-

public void chose_add_view_delete() {
       final CharSequence[] items = { "Add Event", "View Event","Delete Event" };
       AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
       builder.setTitle("What !?");
       builder.setSingleChoiceItems(items, 0,new DialogInterface.OnClickListener() {
         @Override
          public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
                   if (which == 0) {
                   DeleteEvent del = new DeleteEvent();
                   int i = del.DeleteCalendarEntry(82);
                   dialog.dismiss();
}

//////////////////////////////////////////////

09-10 12:28:01.385: E/AndroidRuntime(22297): FATAL EXCEPTION: main
09-10 12:28:01.385: E/AndroidRuntime(22297): java.lang.NullPointerException
09-10 12:28:01.385: E/AndroidRuntime(22297):    at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:90)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at com.iphase.Albayan.DeleteEvent.DeleteCalendarEntry(DeleteEvent.java:25)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at com.iphase.Albayan.MainActivity$9.onClick(MainActivity.java:475)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:935)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at android.widget.ListView.performItemClick(ListView.java:3746)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1980)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at android.os.Handler.handleCallback(Handler.java:587)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at android.os.Looper.loop(Looper.java:130)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at android.app.ActivityThread.main(ActivityThread.java:3691)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at java.lang.reflect.Method.invokeNative(Native Method)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at java.lang.reflect.Method.invoke(Method.java:507)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
09-10 12:28:01.385: E/AndroidRuntime(22297):    at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

1
Uri eventUri = Uri.parse("content://calendar/events");  // or "content://com.android.calendar/events" 


Cursor cursor = contentResolver.query(eventUri, new String[]{"_id"}, "calendar_id = " + calendarId, null, null); // calendar_id can change in new versions 

while(cursor.moveToNext()) {
    Uri deleteUri = ContentUris.withAppendedId(eventUri, cursor.getInt(o));

    contentResolver.delete(deleteUri, null, null);
}
于 2012-11-14T19:37:42.270 に答える