【问题标题】:I Build a Brain Trainer app but Not Showing my TEXTVIEW when i run my app and warning is showing do not concatenate text display with setText我构建了一个 Brain Trainer 应用程序,但当我运行我的应用程序并显示警告时未显示我的 TEXTVIEW 不要将文本显示与 setText 连接
【发布时间】:2016-12-14 06:10:17
【问题描述】:

sumTextView.setText(Integer.toString(a) + " + " + Integer.toString(b)); 

此行显示您在图片中看到的警告..

【问题讨论】:

标签: android android-studio


【解决方案1】:

使用String.format();

sumTextView.setText(String.format("%1$d + %2$d", a, b));

有了这个,你可以用多个变量正确格式化一个字符串,不管它们是字符串还是整数。此示例采用变量 a 的值并用它替换占位符 %1$d。其他变量也是如此。

【讨论】:

    【解决方案2】:

    取一个字符串复制整行,然后在setText中显示字符串

    String str = (Integer.toString(a) + " + " + Integer.toString(a));
    sumTextView.setText(str);
    

    【讨论】:

      【解决方案3】:

      1. 第一个字符串表示不要将字符串与 setText 属性连接。

      String txt = String.valueOf(a) + " + " + String.valueOf(b);
      sumTextView.setText(str);
      

      2. 第二个警告表明,如果 a 或 b 的值为 null 或不是整数,您的程序可能会崩溃或产生异常。 所以检查条件if(a!=null and b!=null) 然后在 if 条件中显示文本。

      【讨论】:

        猜你喜欢
        • 2023-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-14
        • 2021-10-22
        • 1970-01-01
        • 2022-06-14
        相关资源
        最近更新 更多