Вопрос

In my onCreate method i have the following line of code.

CalendarView calendar = (CalendarView) findViewById(R.id.cal);

I'm trying to get the current selected date from this CalendarView and convert it to a String but in the method im using to do this calendar.getDate(); is causing an error.

Это было полезно?

Решение

If you want to access your CalendarView object in all of your class scope (e.g method,subclasses etc.) and use it's properties,methods etc. just create your reference before your onCreate method.

CalendarView calendar;

And then initiliaze it in your onCreate method by accessing your layout.

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); //mylayout refers to your layout that you want to display 
calendar = (CalendarView) findViewById(R.id.calendarid); //Refers to CalendarView that you declared in your layout.

}

And about context just like it's name it keeps the info about either your activies or the application itself.For more info check this.

http://developer.android.com/reference/android/content/Context.html

Другие советы

Actually, once you defined it in the XML, it will be instantiated by Dalvik, you don't need to do that, what you do need to do, is get the reference to the instance. This is done like that:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); // assuming your layout XML is called main
    CalenderView calendar = (CalenderView) findViewById(R.id.theCalendarId); // the ID you gave in the XML
    // Now you can refer to the calendar.
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top