【问题标题】:App works on debug stepping but not on normal debug or run mode应用程序适用于调试步进,但不适用于正常调试或运行模式
【发布时间】:2017-01-28 10:43:19
【问题描述】:

最近我的一个代码遇到了一些问题,现在我设法让它工作,每次运行它都会崩溃。问题是,当我逐步调试时,应用程序可以运行,但是当我取消这些步骤时,它每次都会崩溃......

这是代码:

String r = users.getListRatings();

    userHolder.tx_listratings.setText("     /     " + getAverage(r) + "     \uD83C\uDF1F"); //crashes here: at com.example.juanpablo.coffee.UserListAdapter.getView(UserListAdapter.java:86)


public int getAverage(String args) {
    String a = args;
    String[] x = a.split("");
    int add = 0;
    for (int i = 1; i < x.length; i++) {
        add = add + Integer.parseInt(x[i]); }
    int average = add / a.length(); //crashes here: at com.example.juanpablo.coffee.UserListAdapter.getAverage(UserListAdapter.java:114)
    return average;
}

这是崩溃:java.lang.ArithmeticException: 除以零。

如果可以,请帮助我,如果投反对票,请解释原因,我是新手,需要改进。谢谢。

【问题讨论】:

  • 这是因为 a 那时是一个空字符串。最简单的解决方法是在该语句之前添加一个空字符串检查
  • 但是为什么它在踩踏上起作用呢?
  • 跟javascript有什么关系?
  • 哎呀,对不起....

标签: javascript java android


【解决方案1】:

我通过将 a.length() 更改为 i 来解决此问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    相关资源
    最近更新 更多