0
private static void addToCalendar(ActivityAppointment ctx, final String title, final long dtstart, final long dtend) {
    final ContentResolver cr = ctx.getContentResolver();
    Cursor cursor ;
    if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
        cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    else
        cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    if ( cursor.moveToFirst() ) {
        final String[] calNames = new String[cursor.getCount()];
        final int[] calIds = new int[cursor.getCount()];
        for (int i = 0; i < calNames.length; i++) {
            calIds[i] = cursor.getInt(0);
            calNames[i] = cursor.getString(1);
            cursor.moveToNext();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                ContentValues cv = new ContentValues();
                cv.put("calendar_id", calIds[which]);
                cv.put("title", title);
                cv.put("dtstart", dtstart );
                cv.put("hasAlarm", 1);
                cv.put("dtend", dtend);

                Uri newEvent ;
                if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                    newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
                else
                    newEvent = cr.insert(Uri.parse("content://calendar/events"), cv);

                if (newEvent != null) {
                    long id = Long.parseLong( newEvent.getLastPathSegment() );
                    ContentValues values = new ContentValues();
                    values.put( "event_id", id );
                    values.put( "method", 1 );
                    values.put( "minutes", 15 ); // 15 minutes
                    if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                        cr.insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
                    else
                        cr.insert( Uri.parse( "content://calendar/reminders" ), values );

                }
                dialog.dismiss();
            }

        });

        builder.create().show();
    }
    cursor.close();
}

我在其他一些函数中调用了这个方法

    public void SaveData()
  { 
                .......
              addToCalendar(this, edSubject.getText().toString(), startMillis, endMillis);
}

我的 logcat 错误是:

01-16 17:17:56.740: E/WindowManager(4166): Activity com.sree.weekdayview.calendar.activities.ActivityAppointment has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40810e90 that was originally added here
01-16 17:17:56.740: E/WindowManager(4166): android.view.WindowLeaked: Activity com.sree.weekdayview.calendar.activities.ActivityAppointment has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40810e90 that was originally added here
01-16 17:17:56.740: E/WindowManager(4166):  at android.view.ViewRoot.<init>(ViewRoot.java:291)
01-16 17:17:56.740: E/WindowManager(4166):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:249)
01-16 17:17:56.740: E/WindowManager(4166):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
01-16 17:17:56.740: E/WindowManager(4166):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
01-16 17:17:56.740: E/WindowManager(4166):  at android.view.Window$LocalWindowManager.addView(Window.java:532)
01-16 17:17:56.740: E/WindowManager(4166):  at android.app.Dialog.show(Dialog.java:269)
01-16 17:17:56.740: E/WindowManager(4166):  at com.sree.weekdayview.calendar.activities.ActivityAppointment.SaveData(ActivityAppointment.java:381)
01-16 17:17:56.740: E/WindowManager(4166):  at com.sree.weekdayview.calendar.activities.ActivityAppointment$3.onClick(ActivityAppointment.java:123)
4

1 回答 1

1

由于您显示的对话框,您会收到此错误。对话框与其宿主(活动)的生命周期紧密相关,这意味着通常您不应尝试在专用平台方法之外创建和/或显示对话框。在您的情况下,活动可能在显示对话框时被破坏(例如,由于方向更改),因此出现异常。

有两种方法可以将对话的生命周期绑定到其宿主的生命周期:

  1. 在 API 级别 13 之前,专用平台方法是onCreateDialog()(和showDialog()/ dismissDialog())。它们在活动中可用,并且应该被覆盖以基于 id 创建/显示/关闭对话框。

  2. 从 API 级别 13 开始,不推荐使用上述方法,取而代之的DialogFragmentFragmentManager.

我推荐的第三个选项是将该DialogFragment方法与支持库结合使用。这基本上是选项二,但使用了使现代逻辑能够在运行 Android 1.6 及更高版本的设备上运行的向后移植功能。

有关实现细节,请参阅Android 开发网站上的Dialogs 主题。可以在此处找到使用已弃用方法的示例。

于 2013-01-16T06:50:59.870 回答