如何调试几个简单的 Android 崩溃 - 适合初学者
我对 Android 的东西相当陌生,而且我已经经历了一些简单的崩溃,所以我想向新人传递一些内容,因为我还没有在任何地方看到它发布,它会帮助我阅读关于它在别处。专家可能想忽略其余部分。
1)通过单步执行程序,您发现它崩溃于:
TextView tv = (TextView)findViewById(R.id.something);
您知道“某物”应该是 TextView,但它崩溃了。将其更改为:
View v = findViewById(R.id.something);
TextView tv = (TextView) v;
您可能会发现它在第二行崩溃。将鼠标悬停在“v”上(在 Eclipse 中),它会告诉您可能不是 TextView 的类型。如果您的 XML 文件指定了 TextView 以外的其他内容,则可能会发生这种情况,但我也发现当由于某种原因我的 R.java 文件不同步时也会出现这种情况。如果 XML 没问题,执行“Project | Clean”通常会修复它。或者“某物”可能不是 XML 中的 TextView。
2)您发现,当您向 EditText 写入内容(或更改其他某些小部件)时,会发生崩溃,但该写入语句没有问题。我发现崩溃发生在监听器中对该小部件的更改 - 您可以在监听器中放置一个断点并找到它。
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" 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论