Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3498078
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:26:53+00:00 2026-05-18T12:26:53+00:00

I am using a method to add an event to an internal calendar on

  • 0

I am using a method to add an event to an internal calendar on the phone. Context has always confused me and this time is no different. The method contract wants the application context. The method call is:

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

This is my code to call the method when a button is pressed:

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();
}

}

When I call this from my code I get a force close. The logcat reads:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:26:53+00:00Added an answer on May 18, 2026 at 12:26 pm

    Your problem is that you are creating a Dialog attached to the Application context, which is non-visual. As I see it you don’t need the Application context anyway, since all you’re doing is accessing the content resolver.

    You just need to change this line:

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

    to this:

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

    And the error goes away. (I tested it).

    ADDENDUM:

    Also change the first line of addToCalendar() to use the application context and thereby fulfill the commented requirement, but I’m unconvinced it makes a difference. I.e.:

    final ContentResolver cr = ctx.getApplicationContext().getContentResolver();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I managed to create a class using CodeDom and add a single method to
Should I be using this method of throwing errors: if (isset($this->dbfields[$var])) { return $this->dbfields[$var];
I am designing a method that will add an element to an internal list.
I'm using next method to add a trace record: TraceSource.TraceEvent(TraceEventType, Int32, String) where Int32
Using the method Application.Restart() in C# should restart the current application: but it seems
I am writing a Clone method using reflection. How do I detect that a
I've done scaffolding using the method described here http://wiki.rubyonrails.org/rails/pages/ScaffoldGenerator However, I want to know
I'm deserializing a class called Method using .NET Serialization. Method contains a list of
Is there a way to Invoke an overloaded method using reflection in .NET (2.0).
I am posting a very simple form using a method I have used frequently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.