机器人:检查值是present共享preferences [英] android: check if value is present in Shared Preferences

查看:119
本文介绍了机器人:检查值是present共享preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建共享preferences如下:

I'm creating Shared Preferences as follows

preferences = getSharedPreferences("text", 0);
final Editor editor = preferences.edit();

String s1 = serverIP.getText().toString();
String s2 = serverPort.getText().toString();
String s3 = syncPass.getText().toString();
String s4 = proxyServer.getText().toString();
String s5 = proxyPort.getText().toString();

editor.putString("SERVERIP", s1);
editor.putString("SERVERPORT", s2);
editor.putString("SYNCPASS", s3);
editor.putString("PROXYSERVER", s3);
editor.putString("PROXYPORT", s3);

和我的onCreate要显示在新集的TextView的价值,但我第一次不会存储在共享preferences的值,并且会得到一个空指针异常.... 所以,我想知道,如果有,可以检查共享preferences包含任何值或任何内置的方法没有 ,,,这样我可以检查的价值presence,然后更换新的集textviews与preferences价值。

and onCreate i want to display the values in new set of TextView, but the first time i will not have any values stored in the shared preferences and will get a NULL Pointer exception.... So i want to know if there is any inbuilt method which can check if the shared preferences contains any value or no,,, so that i can check for the value presence and then Replace the New set of textviews with the preferences value..

推荐答案

共享preferences 获取值每种方法都有它的情况下,返回默认值键不存在,

Every method for fetching values from SharedPreferences has default value which is returned in case the key does not exist

preferences = getSharedPreferences("text", 0);
String value = preferences.getString("unknown_key",null);
if (value == null) {
    // the key does not exist
} else {
    // handle the value
}

这篇关于机器人:检查值是present共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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