SharedPreferences getString NULL 参数 [英] SharedPreferences getString NULL parameter

查看:43
本文介绍了SharedPreferences getString NULL 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将 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 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆