I am doing one project which handles lots of images as backgrounds,buttons,etc....sometimes "Out of Memory Error "(BitMap size exceeded error) throws and it clears all the global variabl开发者_如何学JAVAes. So ,when I am referred to global variables my application crashes. How to handle this situation?
You have to create small thumbnail images of your full screen backgrounds and use it for your button backgrounds etc, otherwise you will get this error especially in 2.3 emulator. The images for the main activity background should not be much bigger than needed for the given screen resolution.
If running out of memory is a situation you have a way to handle, enclose the offending code in a try
block, catch
the exception and do whatever is appropriate.
Depending on variables of any scope to survive an activity's destruction and re-creation is not something that will produce good behavior. Your globals are "disappearing" because the uncaught exception causes this cycle to take place, and it will occur in other circumstances such as an orientation change. Your code should be prepared to save and restore its state as described in the Activity
documentation.
精彩评论