ray ray
ray ray

Reputation: 115

getBaseContext() and getApplicationContext()

In the following code, there is an error in getBaseContext() and getApplicationContext(), both of them are red in color.

Does anyone know how to solve them? Moreover, I also want to know when to use these two methods. I have searched in internet, but I didn't understand. Can anyone simply explain something about these two methods? I am new in java.

public class calendar1 extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar_menu);
        Button button1 = (Button) findViewById(R.id.addevent);
        button1.setOnClickListener(new addevent());
        Button button2 = (Button) findViewById(R.id.showcalendar);
        button2.setOnClickListener(new showcalendar());
    }

    private class addevent implements View.OnClickListener {

        @Override
        public void onClick(View v) {

            Calendar cal = Calendar.getInstance();
            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            intent.putExtra("beginTime", cal.getTimeInMillis());
            intent.putExtra("allDay", true);
            intent.putExtra("rrule", "FREQ=YEARLY");
            intent.putExtra("endTime", cal.getTimeInMillis() + 60 * 60 * 1000);
            intent.putExtra("title", "A Test Event from android app");
            startActivity(intent);

        }
    }

    private class showcalendar implements View.OnClickListener{

        public void onClick(View v){

            String[] projection = new String[] { CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION };

            // 0 = January, 1 = February, ...

            Calendar startTime = Calendar.getInstance();
            startTime.set(2014,00,01,00,00);

            Calendar endTime= Calendar.getInstance();
            endTime.set(2015,00,01,00,00);

            // the range is all data from 2014

            String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ))";

            Cursor cursor = this.getBaseContext().getContentResolver().query( CalendarContract.Events.CONTENT_URI, projection, selection, null, null );

            // output the events

            if (cursor.moveToFirst()) {
                do {
                    Toast.makeText(this.getApplicationContext(), "Title: " + cursor.getString(1) + " Start-Time: " + (new Date(cursor.getLong(3))).toString(), Toast.LENGTH_LONG).show();
                } while ( cursor.moveToNext());
            }
        }
    }
}

Upvotes: 0

Views: 680

Answers (1)

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

Use the following context to solve your problem

calendar1.this

To explain why you are getting that error in the first place. You are using

this.getBaseContext() and this.getApplicationContext() inside the showcalendar, which is a View.OnClickListener class, as you can see from here, it does not have those methods.

Upvotes: 1

Related Questions