Android 检查共享首选项是否为null

Android 检查共享首选项是否为null,android,sharedpreferences,Android,Sharedpreferences,在这段代码的帮助下,我正在检查共享首选项值是否为null SharedPreferences myPrefo = this.getSharedPreferences("myPrefo", MODE_PRIVATE); ostt = myPrefo.getString(Sharedprefse.KEY_FSHD, null); if(ostt!= null && !ostt.equals("")) { Toast.makeText(

在这段代码的帮助下,我正在检查共享首选项值是否为null

    SharedPreferences myPrefo = this.getSharedPreferences("myPrefo", MODE_PRIVATE);
    ostt = myPrefo.getString(Sharedprefse.KEY_FSHD, null);

    if(ostt!= null && !ostt.equals(""))
    {
        Toast.makeText(context,"f"+ ostt,                Toast.LENGTH_SHORT).show();

    }
    else{
        Toast.makeText(context, ostt+"n",
                Toast.LENGTH_SHORT).show();

    }
我的问题是,每次它都返回null,即使共享首选项不是空的

这是正确的过程还是我必须尝试其他方法

如果SharedReference为空,那么我将从远程服务器获取一个字符串,并将其存储在sharedpreference中

这是我的密码

   if(ostt == null || ostt.matches("") || ostt.isEmpty()) {
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("Remote Address");
                    HttpResponse response = httpclient.execute(httppost);
                    str = EntityUtils.toString(response.getEntity());

                } catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    if(!(str==null) || !(str.matches("")) || !(str.isEmpty())) {
                        session.createFS(str);
                    }
                }
这是我的createFS()函数

在代码中执行此操作:

要保存到共享首选项,请执行以下操作:

public void createFS(String str){

SharedPreferences sharedPreferences = getSharedPreferences("myPrefo", Context.Mode_Private);
SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString("KEY_FSHD", str);


    editor.commit();
  }
检索时

SharedPreferences myPrefo = this.getSharedPreferences("myPrefo", MODE_PRIVATE);
    ostt = myPrefo.getString("KEY_FSHD", null);

您将获得所需的值。

您如何知道它不是空的?您如何将字符串设置为首选项可能有问题?如何将值保存到共享首选项?如果(!TextUtils.isEmpty())将保存代码发布到共享prefsbetter IF语句
如果(!TextUtils.isEmpty())
将值放置在显示代码的位置,则会强制关闭应用程序您可能会遇到空指针异常,检查str的值?是,我收到空指针异常,但只有当应用程序第一次运行时,因为在第二次运行时,它工作正常。如何消除这个错误??你能猜出为什么会发生这种情况吗?你能告诉我你是在哪一行出错的吗?
SharedPreferences myPrefo = this.getSharedPreferences("myPrefo", MODE_PRIVATE);
    ostt = myPrefo.getString("KEY_FSHD", null);