common java problems when developing with android
DESCRIPTION
For some, developing for the Android platform might provide their first experience of working with a complex, modern Java API. This may test your knowledge of the Java programming language, especially with regard to features such as generics. The Android APIs make use of generics throughout and so you will have to know how to create and handle generic classes.TRANSCRIPT
![Page 1: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/1.jpg)
Software Engineering Large Practical
Common Java problems when coding forAndroid and advice for dealing with them
Stephen Gilmore
School of Informatics, University of Edinburgh
October 24th, 2012
Stephen Gilmore Software Engineering Large Practical
![Page 2: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/2.jpg)
Java problems
For some, developing for the Android platform might provide theirfirst experience of working with a complex, modern Java API. Thismay test your knowledge of the Java programming language,especially with regard to features such as generics. The AndroidAPIs make use of generics throughout and so you will have toknow how to create and handle generic classes.
Stephen Gilmore Software Engineering Large Practical
![Page 3: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/3.jpg)
Raw types
Raw types are a backwards compatibility feature which allowednew generic Java code to use old non-generic libraries and to allownon-generic Java code to continue to compile and work on modernJava versions. Raw types are not needed when working with theAndroid API and your project should not contain them.
Stephen Gilmore Software Engineering Large Practical
![Page 4: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/4.jpg)
Common Java problems: raw types, 8 warnings
shot 2011-11-10 at 14.23.51.png
Stephen Gilmore Software Engineering Large Practical
![Page 5: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/5.jpg)
Quick fix: a bad suggestion
shot 2011-11-10 at 14.24.16.png
Stephen Gilmore Software Engineering Large Practical
![Page 6: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/6.jpg)
Check the constructor documentation
shot 2011-11-10 at 14.26.09.png
Stephen Gilmore Software Engineering Large Practical
![Page 7: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/7.jpg)
Check the class documentation
shot 2011-11-10 at 14.40.52.png
Stephen Gilmore Software Engineering Large Practical
![Page 8: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/8.jpg)
What type of array are we adapting?
shot 2011-11-10 at 14.26.30.png
Stephen Gilmore Software Engineering Large Practical
![Page 9: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/9.jpg)
Type parameter added, 6 warnings
shot 2011-11-10 at 14.26.56.png
Stephen Gilmore Software Engineering Large Practical
![Page 10: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/10.jpg)
Raw type parameters
Raw types need special treatment in method headers wheninstances of generic classes are being passed as parameters tomethods. A special syntax is used to specify when we do not knowthe type of the parameter to the generic class.
Stephen Gilmore Software Engineering Large Practical
![Page 11: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/11.jpg)
Raw type in method header, 5 warnings
shot 2011-11-10 at 14.57.11.png
Stephen Gilmore Software Engineering Large Practical
![Page 12: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/12.jpg)
Consult the Java documentation
shot 2011-11-10 at 14.59.44.png
Stephen Gilmore Software Engineering Large Practical
![Page 13: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/13.jpg)
Adapter doesn’t work here
shot 2011-11-10 at 14.58.23.png
Stephen Gilmore Software Engineering Large Practical
![Page 14: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/14.jpg)
Adapter is an interface
shot 2011-11-10 at 15.00.03.png
Stephen Gilmore Software Engineering Large Practical
![Page 15: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/15.jpg)
T is not a class
shot 2011-11-10 at 15.29.34.png
Stephen Gilmore Software Engineering Large Practical
![Page 16: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/16.jpg)
Object: the goto class
shot 2011-11-10 at 14.58.06.png
Stephen Gilmore Software Engineering Large Practical
![Page 17: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/17.jpg)
T extends Adapter doesn’t work
shot 2011-11-10 at 15.29.09.png
Stephen Gilmore Software Engineering Large Practical
![Page 18: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/18.jpg)
“?” — the wild card parameter, 3 warnings
shot 2011-11-10 at 15.44.02.png
Stephen Gilmore Software Engineering Large Practical
![Page 19: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/19.jpg)
“Quick fix” would have worked here
shot 2011-11-10 at 16.11.46.png
Stephen Gilmore Software Engineering Large Practical
![Page 20: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/20.jpg)
Same result
shot 2011-11-10 at 16.11.57.png
Stephen Gilmore Software Engineering Large Practical
![Page 21: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/21.jpg)
Working with the Java compiler
The Java compiler used Eclipse allows you to tune the level ofchecking which your program receives during compilation. Thischecking (“static analysis”) evaluates the correctness of yourprogram without executing it.
The default settings do not apply particularly strict checking. Werecommend tightening the default settings to detect errors in yourJava code which you may otherwise miss.
Stephen Gilmore Software Engineering Large Practical
![Page 22: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/22.jpg)
Setting Java compiler preferences
shot 2011-11-11 at 09.00.58.png
Stephen Gilmore Software Engineering Large Practical
![Page 23: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/23.jpg)
Changing defaults
shot 2011-11-11 at 09.01.20.png
Stephen Gilmore Software Engineering Large Practical
![Page 24: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/24.jpg)
Potential programming problems
shot 2011-11-11 at 09.01.34.png
Stephen Gilmore Software Engineering Large Practical
![Page 25: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/25.jpg)
Tighter checking
shot 2011-11-11 at 09.02.19.png
Stephen Gilmore Software Engineering Large Practical
![Page 26: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/26.jpg)
Bug found
shot 2011-11-11 at 09.10.40.png
Stephen Gilmore Software Engineering Large Practical
![Page 27: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/27.jpg)
Remembering and forgetting while developing
Leaving an empty block is a classic “I must remember to do thatlater” error. By turning on stricter checking with the staticanalysis tools in the Java compiler we get automatic reminders inthe form of Java problems. These will persist in our project untilwe do go back and write the remaining code block.
Stephen Gilmore Software Engineering Large Practical
![Page 28: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/28.jpg)
Logging
As we have seen, the Android emulator occupies all of the screenreal estate of our virtual device, in order to allow us to test apps asthey will be used in practice. If we want to log informationalmessages for our own purposes then we should use the Androidlogging API.
Stephen Gilmore Software Engineering Large Practical
![Page 29: Common Java problems when developing with Android](https://reader036.vdocument.in/reader036/viewer/2022062705/55660fbad8b42a7d608b45e1/html5/thumbnails/29.jpg)
Don’t forget to add logging . . .
shot 2011-11-11 at 10.41.02.png
Stephen Gilmore Software Engineering Large Practical