Android的 - 共享preferences不工作? [英] Android - SharedPreferences not working?

查看:135
本文介绍了Android的 - 共享preferences不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的code从哪里获得有共享preferences

Here's the code where I get to have the SharedPreferences:

public class MainActivity2 extends Activity {
Button b;
EditText et;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
String rfid, getPref;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity2);




    b = (Button)findViewById(R.id.loginnext);
    et = (EditText)findViewById(R.id.rfid);
    tv = (TextView)findViewById(R.id.tv);



    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog = ProgressDialog.show(MainActivity2.this, "",
                    "Validating user...", true);
            new Thread(new Runnable() {
                public void run() {
                    login();
                }
            }).start();
        }
    });
}

void login(){
    try{

        SharedPreferences preferences = getSharedPreferences(getPref, Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = preferences.edit();

        httpclient=new DefaultHttpClient();
        httppost= new HttpPost("http://usamobileapp.pe.hu/webservice/check.php");

        nameValuePairs = new ArrayList<NameValuePair>(2);

        nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        response=httpclient.execute(httppost);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        System.out.println("Response : " + response);
        runOnUiThread(new Runnable() {
            public void run() {
                tv.setText("Response from PHP : " + response);
                dialog.dismiss();
            }
        });

        if(response.equalsIgnoreCase("User Found")){
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(MainActivity2.this,"Login Success", Toast.LENGTH_SHORT).show();

                    editor.putString("rfid" ,rfid);
                    editor.commit();

                }
            });

            startActivity(new Intent(MainActivity2.this, MainActivity3Activity.class));
        }else{
            showAlert();
        }

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
}

这就是我想要获得注册ID用户输入,并在应用程序的所有活动中使用它。

This is where I'd like the get the Registration ID entered by the user and use it in all activities of the app.

下面是code,检查我是否可以得到以正确的有共享preferences。这显示它经过我的文本视图。我不知道为什么它不会显示出来。我做了分享preferences 是否正确?

Here is the code to check whether I get to have the SharedPreferences correctly. It displays it through I text view. I wonder why it would not show up. Did I do the SharePreferences correctly?

public class ViewGradesActivity extends ActionBarActivity {

TextView viewPref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_grades);

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    String get = settings.getString("rfid", "");

    viewPref = (TextView) findViewById(R.id.tv);
    viewPref.setText(get);

}

}

顺便说一句,在code以上是从另一个类。

By the way, the code above is from another class.

感谢您提前帮助。

推荐答案

由于你正在写一个不同的共享preferences您收到此错误比你从阅读。如果您使用的是默认的共享preferences ,请确保您读取和写入到默认的preferences。

You are receiving this error because you are writing to a different SharedPreferences than you are reading from. If you are using default SharedPreferences, make sure you read and write to the default preferences.

// Read the String from SharedPreferences
String awesomeString = PreferenceManager.getDefaultSharedPreferences(context).getString("key", "");

// Write the String to SharedPreferences
PreferenceManager.getDefaultSharedPreferences(context)
        .edit().putString("key", awesomeString).commit();

这篇关于Android的 - 共享preferences不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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