如果我将 SharedPreferences.getString 的第二个参数设置为 NULL,我会在某些设备上收到错误(异常)吗?
Will I receive an error (Exception) on some devices if I set the second parameter of SharedPreferences.getString NULL?
SharedPreferences settings = ... String data = settings.getString(_.PREFIX , null);它是否会导致至少一台设备出现异常或错误?或者我必须将这部分代码包装在 try-catch 块中?
Will it cause an exception or an error on at least one device? Or I have to wrap this part of code in try-catch block?
推荐答案String data = settings.getString(_.PREFIX , null/Null here is default value/);null - 当您的 SraredPreferences 没有此项目时,您可以接收(例如,如果您在将此字段设置为任何信息或用户从设备设置中清除应用程序现金之前调用/获取此字符串).我认为这可能是正常情况,如果您希望得到它(一些 emum 字段),您可以使用一些默认值删除null".如果您不认为在使用前获取空验证数据.我认为您的应用程序必须准备好获得两种变体,因为用户可以更改正常工作流程.
String data = settings.getString(_.PREFIX , null/Null here is default value/); null - u can receive when your SraredPreferences have not this item(For example if u call/get this string before setting to this field any info or user clear cash of application from settings of device). I think it's can be normal situation, and u can remove "null" with some default value if you hope to got it(some emum field). If u don't suppose get null validate data before using. I thin'k your app must be ready get both variant, because user can change normal workflow.
更多推荐
SharedPreferences getString NULL 参数
发布评论