How to debug a couple of simple android crashes - for beginners
I'm fairly new at Android stuff, and I've slogged through a few simple crashes, so I wanted to pass along a a couple of items to new guys because I haven't seen it posted anywhere and it would have helped me to read about it elsewhere. Experts might want to ignore the rest of this.
1) By stepping the program, you find that it crashes at:
TextView tv = (TextView)findViewById(R.id.something);
You know that the 'something' should be a TextView but it crashes. Change it to be:
View v = findViewById(R.id.something);
TextView tv = (TextView) v;
You'll probably find it crashes on the second line. Hover (in Eclipse) over the "v开发者_JS百科" and it'll tell you the type which might not be a TextView. This can happen if your XML file specifies something other than a TextView, but I also found this to be the case when for some reason my R.java file was out of sync. If the XML is OK, doing a "Project | Clean" will often fix it. Or maybe 'something' is not a TextView in your XML.
2) You find that when you write something to an EditText (or change some other widget), you get a crash, but that write statement is fine. I discovered that the crash was in the listener on a change to that widget - you can put a breakpoint in the listener and find it.
精彩评论