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

表示しているダイアログが原因で、このエラーが発生します。ダイアログは、そのホスト (アクティビティ) のライフ サイクルに密接に結び付けられています。つまり、通常、専用のプラットフォーム メソッド以外でダイアログを作成したり、表示したりしないでください。あなたの場合、ダイアログが表示されている間にアクティビティが破棄される可能性があります(たとえば、方向の変更のため)。したがって、例外です。

ダイアログのライフサイクルをそのホストのライフサイクルにバインドするには、2 つの方法があります。

  1. API レベル 13 まで、専用のプラットフォーム メソッドはonCreateDialog()(およびshowDialog()/ dismissDialog()) でした。これらはアクティビティ内で使用でき、ID ベースでダイアログを作成/表示/非表示にするためにオーバーライドする必要があります。

  2. API レベル 13 以降、上記のメソッドは非推奨となりDialogFragment、ライフサイクルが によって管理されるダイアログのフラグメント ラッパーである が優先されFragmentManagerます。

私がお勧めする 3 番目のオプションは、このDialogFragmentアプローチをサポート ライブラリと組み合わせて使用​​することです。これは基本的にオプション 2 ですが、最新のロジックが Android 1.6 以降を実行しているデバイスで動作できるようにするバックポート機能を使用します。

実装の詳細については、Android 開発者サイトのダイアログのトピックを参照してください。非推奨のアプローチを使用した例は、こちらにあります

于 2013-01-16T06:50:59.870 に答える