Trouble with final


#1

First of all, hi there and thank you for a fantastic book so far!

So here is the problem that I am having. I am sorry in advance if there is something silly and/or stupid that I missed because I am new to the game.

(Using Android Studio 0.3.1 on Win 8.1 running Java V7 U45 and targeting Android SDK v19)
The compiler tells me that the variables within “onCreateDialog” (see below) must all be declared final in order to be used as parameters within the inner class…

int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
mDate = new GregorianCalendar(year, month, day).getTime();

Declaring these variables as final however seems to stop the dates to flow. In particular, I cannot change anything but the year via the DatePicker. Do you have any idea what I might have done wrong? Should I provide more code? Thank you for any sort of hint or advice. I would really appreciate it.


#2

Thanks for the kind words! As for your problem, try checking the names of the parameters on the onDateChanged listener versus what we show in the book.


#3

I just ran into this myself. As phillips pointed out, the onDateChanged parameters–year, month, day–have the same names as the local variables. Try renaming the onDateChanged parameter names to anything other than ‘year’, ‘month’, ‘day’:

DatePicker datePicker = (DatePicker)view.findViewById(R.id.dialog_date_datePicker); datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker datePicker, int y, int m, int d) { mDate = new GregorianCalendar(y, m, d).getTime();


#4

Hi,

totally agree with all the GREAT BOOK acknowledgements!

Thanx for the tip above - make some sence - but does not solve the issue for me - the date from the picker is not updated into the CrimeFragment view.

Now on Android Studio 0.3.5… Any other suggestion?


#5

@phillips and ssimon : Thanks for the reply. I got it figured out. I guess it was late that night and my eyes must have been crazy tired. Sorry for that!

@heleneapps : It seems that you are having another type of problem. I think you should provide a little bit more information i.e. show some of the code that might be troubling you.


#6

My troubles seems to be gone, cleaning cache, upgrading, I do not know the details (but I do know Android Studio is a preview product still…)

:slight_smile:


#7

Hi there,

I solved the problem as this:

The function onDateChanged() is filled with the datePicker and three int’s for year, month and day. But to use these commited arguments, you have to name them and not year, month and day.

Android Studio shows these three Arguments to me as i, i2 and i3, so the call on mDate should be:

mDate = new GregorianCalendar(i,i2,i3).getTime();

and NOT

mDate = new GregorianCalendar(year, month, day).getTime();

hope to help you guys!

Jan