What is the main cause of force close on android? Is 开发者_如何学Cthere any way to help avoid unwanted force close problem when coding an android application?
Most causes for Runtime-Exceptions are Nullpointers. Always avoid those problems! That is very necessary.
Other error-cases - especially in android - are mostly using a wrong or missing context from one to another activity.
Testing by hand and using the Testing-Framework JUnit will result in software with less errors. Just use them the right way:)
Always have a habit of surrounding your code(unsure) with try/catch block and also trace the exception to know why it is happening
try{
// your code where you are unsure what is the output
}catch(exception e)
{
e.printStackTrace();
}
A fairly common mistake I see is not closing Database connections.
Though from your question it really could be anything which is going wrong, and Steve is correct in that the best way to find out excatly what is wrong is to use the debugger in eclipse.
Are you using the debugger? It will tell you what's going on. Quite simple to use in Eclipse by opening a DDMS view, then choosing your phone in the devices panel... then you can see all the logged messages in the LogCat window. Look for red text.
edit: I can't answer what the main cause of Force Closes is - I have had them for all kinds of little errors (and a few huge ones as well). Its one aspect of Android developing that can be a little frustrating - but the debugger really does help cast light on the matter. Start at the top of the error section (which will tell you what the error is) and work down until you find a reference to a line in your code - that will most likely be the culprit that caused it.
Hope that helps
Steve
1 - Runtime Exceptions + AndroidRuntimeException
2 - UI Threads Stucks for 5 seconds (NRD - Not Responding Dialog shows)
Any unhandeled exception will lead to the "force close" screen - for details you need to open a debug screen.
In Eclipse: Window -> Show View -> Other -> Android -> LogCat
Watch out for exceptions here
精彩评论