I have a problem with my calendar. Here is the code:
Calendar mCalendar = Calendar.getInstance();
mToday[0] = mCalendar.get(Calendar.DAY_OF_MONTH);
mToday[1] = mCalendar.get(Calendar.MONTH); // zero based
mToday[2] = mCalendar.get(Calendar.YEAR);
Can not run my project because AndroidStudio shows error on the Calendar.DAY_OF_MONTH
, Calendar.MONTH
etc... I get nullPointException while running in emulator
It sais that
Must be one of: java.util.Calendar.DAY_OF_MONTHjava.util.Calendar.MONTH etc...
I don't understand this error because Calendar.MONTH
is one of java.util.calendar.MONTH
I have an import for it
import java.util.Calendar;
Sorry I missed that it's initialized but there is an other class where I want to use Calendar.MONTH and so on like this:
mCalendarToday = Calendar.getInstance();
...
int dayOfWeek = mCalendar.get(Calendar.DAY_OF_WEEK);
int firstDay = getDay(mCalendar.get(Calendar.DAY_OF_WEEK);
OR
private Calendar mCalendarToday;
...
private boolean isToday(int day, int month, int year) {
if (mCalendarToday.get(Calendar.MONTH) == month
&& mCalendarToday.get(Calendar.YEAR) == year
&& mCalendarToday.get(Calendar.DAY_OF_MONTH) == day) {
return true;
}
return false;
}
Here shows error too. And all the other lines where I would use.
I do believe that the way you access the calendar is correct.
I'm guessing the int[]
you try to add the values to is not initialized. You must call
int[] mToday = new int[3];
Hardcoded. But must work.
int[] mToday = new int[]{};
java.util.Calendar calendar = java.util.Calendar.getInstance();
mToday[0] = calendar.get(java.util.Calendar.DAY_OF_MONTH);
mToday[1] = calendar.get(java.util.Calendar.MONTH);
mToday[2] = calendar.get(java.util.Calendar.YEAR);
maybe you did not initialize you field. I tried it in Eclipse and this worked
int[] mToday = new int[10];
Calendar mCalendar = Calendar.getInstance();
mToday[0] = mCalendar.get(Calendar.DAY_OF_MONTH);
mToday[1] = mCalendar.get(Calendar.MONTH); // zero based
mToday[2] = mCalendar.get(Calendar.YEAR);
It looks like a bug of Android Studio: 0.5.5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.