AndroidStudio Calendar.get(Calendar.DAY_OF_MONTH) nullPointer Exception

前端 未结 4 1369
傲寒
傲寒 2020-12-22 06:10

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]         


        
相关标签:
4条回答
  • 2020-12-22 06:40

    It looks like a bug of Android Studio: 0.5.5

    see http://code.google.com/p/android/issues/detail?id=68758

    0 讨论(0)
  • 2020-12-22 06:42

    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);
    
    0 讨论(0)
  • 2020-12-22 06:50

    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];
    
    0 讨论(0)
  • 2020-12-22 06:50

    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);
    
    0 讨论(0)
提交回复
热议问题