【发布时间】: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