Android 共享首选项值返回空

Android 共享首选项值返回空,android,sharedpreferences,android-sharedpreferences,Android,Sharedpreferences,Android Sharedpreferences,我正在从服务器获取值并保存到共享首选项,然后将其检索到字符串中,但该值为空。有人能告诉我出了什么问题吗?以及如何比较通道和链接字符串中的值的建议。 }在editor.commit()之后尝试使用下面的代码:- editor.apply(); 试试这个 public class MainActivity extends Activity { SharedPreferences sharedpreferences; EditText name; EditText ema

我正在从服务器获取值并保存到共享首选项,然后将其检索到字符串中,但该值为空。有人能告诉我出了什么问题吗?以及如何比较通道和链接字符串中的值的建议。


}

editor.commit()之后尝试使用下面的代码:-

editor.apply();
试试这个

 public class MainActivity extends Activity {
    SharedPreferences sharedpreferences;
    EditText name;
    EditText email;
    public static final String mypreference = "mypref";
    public static final String Name = "nameKey";
    public static final String Email = "emailKey";
    private static String NAMESPACE = "http://telview360/";
    private static String URL = "http://54.179.134.139/viView360Service/WebService.asmx?WSDL";
    private static String SOAP_ACTION = "http://telview360/ImageDetails";
    private static String METHOD_NAME = "ImageDetails";
    String link;
    String channel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Thread networkThread = new Thread() {
            @Override
            public void run() {
                try {
                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE ht = new HttpTransportSE(URL);
                    ht.call(SOAP_ACTION, envelope);
                    final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                    link = response.toString();
                    UpdateLinkValue(link);
                    Log.d("Web response", ":" + link);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        networkThread.start();
        try {
            Thread.currentThread().sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    channel = getPrefLink(MainActivity.this);
    Log.d(" value",":"+channel);
    }

    public void UpdateLinkValue(String link){
          String prevLink = getPrefLink(MainActivity.this);
          if(!prevLink.lenght() > 0 || !prevLink.equals(link))
              setPrefLink(MainActivity.this,link);
    }
    public void setPrefLink(Context context, String link) {
            SharedPreferences.Editor editor = getPreferencesEditor(context);
            editor.putString(mypreference , link);        
            editor.apply();
            editor.commit();
        }

public SharedPreferences getPreferences(Context context) {
        return context.getSharedPreferences(mypreference, Context.MODE_PRIVATE);
    }

    public String getPrefLink(Context context) {          
            return getPreferences(context).getString(mypreference , "");
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    }
如果您使用函数保存首选项并获取首选项,则可以从任何位置调用它们。

尝试以下操作:-

public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
EditText name;
EditText email;
public static final String mypreference = "mypref";
public static final String Name = "nameKey";
public static final String Email = "emailKey";
private static String NAMESPACE = "http://telview360/";
private static String URL = "http://54.179.134.139/viView360Service/WebService.asmx?WSDL";
private static String SOAP_ACTION = "http://telview360/ImageDetails";
private static String METHOD_NAME = "ImageDetails";
String link;
String channel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Thread networkThread = new Thread() {
        @Override
        public void run() {
            try {
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.setOutputSoapObject(request);
                HttpTransportSE ht = new HttpTransportSE(URL);
                ht.call(SOAP_ACTION, envelope);
                final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                link = response.toString();

sharedpreferences = getSharedPreferences(mypreference,
            Context.MODE_PRIVATE);

    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putString(Name, link);
    editor.commit();
    Log.d("Saved value",":"+editor.commit());



    channel = sharedpreferences.getString(Name, "");
    Log.d(" value",":"+channel);

                Log.d("Web response", ":" + link);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    networkThread.start();
    try {
        Thread.currentThread().sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }




}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

检查
链接的
值是否为空或是否有任何值?您可以在logcat中的web响应中看到一个来自服务器的值,在link=response.toString()行之后在线程内写入您的SharedReference;commit();将工作,但为了更好的性能,请使用editor.apply();感谢@Mohammad nabil它现在重新获得了值,如果链接值被更改,我还需要一个建议我需要用链接的新值替换频道中的值如何比较它们这将立即保存并在之后快速使用。getPreferencesEditor(上下文)无法解决此问题,它说,如果链接值与通道中的存储值进行比较并更新,则我现在可以获得值和一个建议,如果链接值与通道中的存储值进行比较并更新,则我现在可以获得值和一个建议
public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
EditText name;
EditText email;
public static final String mypreference = "mypref";
public static final String Name = "nameKey";
public static final String Email = "emailKey";
private static String NAMESPACE = "http://telview360/";
private static String URL = "http://54.179.134.139/viView360Service/WebService.asmx?WSDL";
private static String SOAP_ACTION = "http://telview360/ImageDetails";
private static String METHOD_NAME = "ImageDetails";
String link;
String channel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Thread networkThread = new Thread() {
        @Override
        public void run() {
            try {
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.setOutputSoapObject(request);
                HttpTransportSE ht = new HttpTransportSE(URL);
                ht.call(SOAP_ACTION, envelope);
                final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                link = response.toString();

sharedpreferences = getSharedPreferences(mypreference,
            Context.MODE_PRIVATE);

    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putString(Name, link);
    editor.commit();
    Log.d("Saved value",":"+editor.commit());



    channel = sharedpreferences.getString(Name, "");
    Log.d(" value",":"+channel);

                Log.d("Web response", ":" + link);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    networkThread.start();
    try {
        Thread.currentThread().sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }




}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}