我已经知道是什么导致了这个错误,我只是不知道如何处理用户没有在对话框中输入任何内容然后点击将字符串解析为 int 的按钮的情况。它无法将空字符串解析为 int,因此会抛出错误。我已经对如何执行此操作进行了一些研究,但没有找到令人满意的有效结果。
问题:在尝试运行其余代码之前,如何检查对话框中是否包含文本。
最佳答案
一些代码对语法有帮助,但基本上
if ("".equals(text) // where text is the text that you get from an EditText or wherever you get it
{ // give message to enter valid text; }
此外,您可以用 try/catch
包围并捕获 numberFormatException,然后打印适当的消息
关于android - java.lang.NumberFormatException : Invalid int: "" in android 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16726429/