首页 > 解决方案 > IllegalArgumentException 将在添加 String 值时显示错误

问题描述

我是 Android 开发新手,这是一个基本问题。

我试图用字符串值替换 IllegalArgumentException。如何替换不会显示Android错误的这个?需要帮忙。

它不会工作:

R.string.msg_log1

getString(R.string.msg_log1)

我的代码如下:

          if (myPhone == null || myPhone < 0) {
            throw new IllegalArgumentException("Data requires valid phone");

标签: androidillegalargumentexception

解决方案


如果你在一个活动中,你可以像这样使用它。

if (myPhone == null || myPhone < 0){ 
    Toast.makeText(this, getResources().getString(R.string.msg_log1), Toast.LENGTH_SHORT).show();
}

确保将名为 msg_log1 的字符串添加到 strings.xml


推荐阅读