【发布时间】:2016-12-14 06:10:17
【问题描述】:
sumTextView.setText(Integer.toString(a) + " + " + Integer.toString(b));
此行显示您在图片中看到的警告..
【问题讨论】:
-
不明白问题,请贴xml文件或日志文件。
sumTextView.setText(Integer.toString(a) + " + " + Integer.toString(b));
此行显示您在图片中看到的警告..
【问题讨论】:
使用String.format();
sumTextView.setText(String.format("%1$d + %2$d", a, b));
有了这个,你可以用多个变量正确格式化一个字符串,不管它们是字符串还是整数。此示例采用变量 a 的值并用它替换占位符 %1$d。其他变量也是如此。
【讨论】:
取一个字符串复制整行,然后在setText中显示字符串
String str = (Integer.toString(a) + " + " + Integer.toString(a));
sumTextView.setText(str);
【讨论】:
1. 第一个字符串表示不要将字符串与 setText 属性连接。
String txt = String.valueOf(a) + " + " + String.valueOf(b);
sumTextView.setText(str);
2. 第二个警告表明,如果 a 或 b 的值为 null 或不是整数,您的程序可能会崩溃或产生异常。
所以检查条件if(a!=null and b!=null) 然后在 if 条件中显示文本。
【讨论】: