android - 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");
解决方案
如果你在一个活动中,你可以像这样使用它。
if (myPhone == null || myPhone < 0){
Toast.makeText(this, getResources().getString(R.string.msg_log1), Toast.LENGTH_SHORT).show();
}
确保将名为 msg_log1 的字符串添加到 strings.xml
推荐阅读
- leaflet - 传单 - 稍后按属性单击添加删除 GeoJSON 多边形?
- selenium-chromedriver - TestNG - Chrome 版本 79 的 ChromeDriver - 防止恶意代码访问
- ruby - 在 Thread Ruby 中访问模块
- linq - Linq Query Select from table1 where id in (select from table2 inner join table3 on table2 = table3 where table3 = id)
- angular - Angular - 根据另一个对象数组中的值过滤对象数组
- python - Pypy 在 Raspberry Pi 上找不到 Flask
- azure-devops - 创建 YAML 后如何创建构建完成触发器
- r - ckanr 函数 ds_search_sql 或替代
- html - 将元素放入容器的问题
- android - Flutter中成功的firebase身份验证发生错误