1

我正在使用一种方法将事件添加到手机上的内部日历。上下文一直让我感到困惑,这次也不例外。方法契约需要应用程序上下文。方法调用是:

addToCalendar(Context context, String title, long dtstart, long dtend);

这是我在按下按钮时调用该方法的代码:

public class DateAdder extends Activity {
String shiftType;
Date d = new Date();


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 




    Button AShift = (Button) findViewById(R.id.AShift);
    AShift.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            TextView BtmTxt = (TextView) findViewById(R.id.BtmTxt);
            shiftType="A Shift";
            addToCalendar(DateAdder.this.getApplicationContext(), shiftType, d.getDate(), d.getDate()+1000);                
            BtmTxt.setText("Done");
        }});
} //END of onCreate



/**
 * Adds the event to a calendar. It lets the user choose the calendar
 * @param ctx Context ( Please use the application context )
 * @param title title of the event
 * @param dtstart Start time: The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.
 * @param dtend End time: The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.
 */
private static void addToCalendar(Context 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() {

            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("dtend", dtend);
                cv.put("allday", 1);
                cv.put("hasAlarm", 0);

                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://com.android.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 minuti
                    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.cancel();
            }

        });

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

}

当我从我的代码中调用它时,我会强制关闭。日志猫读取:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
4

4 回答 4

3

您的问题是您正在创建一个附加到应用程序上下文的对话框,它是非可视的。正如我所见,无论如何您都不需要应用程序上下文,因为您所做的只是访问内容解析器。

您只需要更改此行:

addToCalendar(DateAdder.this.getApplicationContext(), 
    shiftType, d.getDate(), d.getDate()+1000);

对此:

addToCalendar(DateAdder.this, 
    shiftType, d.getDate(), d.getDate()+1000);

并且错误消失了。(我测试过)。

附录:

还要更改 addToCalendar() 的第一行以使用应用程序上下文,从而满足注释的要求,但我不相信它会有所作为。IE:

final ContentResolver cr = ctx.getApplicationContext().getContentResolver();
于 2010-12-10T14:43:29.943 回答
0

改变这个

addToCalendar(getApplicationContext(), shiftType, d.getDate(), d.getDate()+1000);

addToCalendar(---yourClassName---.this.getApplicationContext(), shiftType, d.getDate(), d.getDate()+1000); 

一个例子 :

addToCalendar(MainActivity.this.getApplicationContext(), shiftType, d.getDate(), d.getDate()+1000); 
于 2010-12-10T11:42:28.693 回答
0

如果你在一个活动中,你可以直接传递this而不是getApplicationContext,因为活动扩展了上下文。ApplicationContext 是整个应用程序的上下文,而传递this允许您传递当前上下文。这可能会解决您的问题。

于 2010-12-10T12:38:00.863 回答
0

有时你也可以这样做

(活动)view.getParent()

而不是 Dateadder.this 或 dateadder.this.getapplicationContext()

于 2010-12-10T22:09:28.703 回答