如何调试几个简单的 Android 崩溃 - 适合初学者

发布于 2024-12-03 22:21:38 字数 662 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文