common java problems when developing with android

29
Software Engineering Large Practical Common Java problems when coding for Android and advice for dealing with them Stephen Gilmore School of Informatics, University of Edinburgh October 24th, 2012 Stephen Gilmore Software Engineering Large Practical

Upload: stephen-gilmore

Post on 28-May-2015

555 views

Category:

Education


1 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

“?” — 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

“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

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

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

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

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

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

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

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

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

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

Don’t forget to add logging . . .

shot 2011-11-11 at 10.41.02.png

Stephen Gilmore Software Engineering Large Practical