我正在尝试从共享首选项中获取值,并且它返回null。共享首选项值是从asynctask设置的。我在asynctask中测试它,在toast中打印stord值并且它工作正常,但问题是当我尝试通过单击buttonRegister在asynctask外部检索值时,它显示为null。 logcat中没有显示错误。
活动代码:
package com.example.mohalogin;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Properties;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends Activity {
private EditText editTextName;
private EditText editTextUsername;
private EditText editTextPassword;
private EditText editTextEmail;
private Button buttonRegister;
private Button buttonLogin;
//private Button gmail,yahoo;
Context context;
private SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPreferences" ;
public static final String RegisResult = "result";
private static final String REGISTER_URL = "xxxxxxxxxx"; //fake data
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextUsername = (EditText) findViewById(R.id.editTextUserName);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
buttonLogin = (Button) findViewById(R.id.buttonLogin);
buttonRegister.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
registerUser();
sharedpreferences=getApplicationContext().getSharedPreferences("MyPrefs", getApplicationContext().MODE_PRIVATE);
buttonLogin.setText(sharedpreferences.getString("result","")); //testing by printing the value in the button
}
});
/*buttonLogin.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
}
});*/
}
private void registerUser() {
String name = editTextName.getText().toString().trim().toLowerCase();
String username = editTextUsername.getText().toString().trim().toLowerCase();
String password = editTextPassword.getText().toString().trim().toLowerCase();
String email = editTextEmail.getText().toString().trim().toLowerCase();
register(name,username,password,email);
}
private void register(String name, String username, String password, String email) {
class RegisterUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RegisterUserClass ruc = new RegisterUserClass();
private Context context2;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(RegisterActivity.this, "Please Wait","Registering new user", true, false);
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String,String>();
data.put("name",params[0]);
data.put("username",params[1]);
data.put("password",params[2]);
data.put("email",params[3]);
String result = ruc.sendPostRequest(REGISTER_URL,data);
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
sharedpreferences = context2.getSharedPreferences(MyPREFERENCES, context2.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(RegisResult, s);
editor.commit();
Toast.makeText(getApplicationContext(),sharedpreferences.getString("result",""), Toast.LENGTH_LONG).show();
}
public RegisterUser(Context context2)
{
this.context2=context2;
}
}
RegisterUser ru = new RegisterUser(getApplicationContext());
ru.execute(name, username, password, email);
}
}
&#13;
答案 0 :(得分:1)
当您指定要由AsyncTask执行的部分代码时,它实际上是在除Ui线程之外的新线程中执行的。实际上在 ButtonRegister 的 onClick , registerUser()和以下两行,在并行模式不是顺序。
如果要确保成功完成SharedPreferences中的保存,可以再次阅读并设置按钮文本。以这种方式更改AsyncTask的 OnPostExecute :
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
loading.dismiss();
.
.
.
buttonLogin.setText(sharedpreferences.getString("result",""));
}
答案 1 :(得分:1)
你的问题是调用getSharedPreferences(String,int) -
您基本上使用两个单独的SharePreference实例,因为每次都为SharedPreferences传递一个不同的名称(String) -
存储值时,您正在使用
getSharedPreferences("MyPreferences", context2.MODE_PRIVATE);
当您尝试获取该值时,您正在使用
getSharedPreferences("MyPrefs", getApplicationContext().MODE_PRIVATE);
修改您的代码以使用相同的SharedPreference名称,它应该有效。
答案 2 :(得分:0)
您正在错误的位置访问该值。 请尝试使用此代码:)
package com.example.mohalogin;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Properties;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends Activity {
private EditText editTextName;
private EditText editTextUsername;
private EditText editTextPassword;
private EditText editTextEmail;
private Button buttonRegister;
private Button buttonLogin;
//private Button gmail,yahoo;
Context context;
private SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPreferences" ;
public static final String RegisResult = "result";
private static final String REGISTER_URL = "xxxxxxxxxx"; //fake data
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
sharedpreferences=getApplicationContext().getSharedPreferences("MyPrefs", getApplicationContext().MODE_PRIVATE);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextUsername = (EditText) findViewById(R.id.editTextUserName);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
buttonLogin = (Button) findViewById(R.id.buttonLogin);
buttonRegister.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
registerUser();
}
});
/*buttonLogin.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
}
});*/
}
private void registerUser() {
String name = editTextName.getText().toString().trim().toLowerCase();
String username = editTextUsername.getText().toString().trim().toLowerCase();
String password = editTextPassword.getText().toString().trim().toLowerCase();
String email = editTextEmail.getText().toString().trim().toLowerCase();
register(name,username,password,email);
}
private void register(String name, String username, String password, String email) {
class RegisterUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RegisterUserClass ruc = new RegisterUserClass();
private Context context2;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(RegisterActivity.this, "Please Wait","Registering new user", true, false);
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String,String>();
data.put("name",params[0]);
data.put("username",params[1]);
data.put("password",params[2]);
data.put("email",params[3]);
String result = ruc.sendPostRequest(REGISTER_URL,data);
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute();
loading.dismiss();
sharedpreferences = context2.getSharedPreferences(MyPREFERENCES, context2.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(RegisResult, s);
editor.commit();
Toast.makeText(getApplicationContext(),sharedpreferences.getString("result",""), Toast.LENGTH_LONG).show();
buttonLogin.setText(sharedpreferences.getString(RegisResult,"")); //testing by printing the value in the button
}
public RegisterUser(Context context2)
{
this.context2=context2;
}
}
RegisterUser ru = new RegisterUser(getApplicationContext());
ru.execute(name, username, password, email);
}
}
答案 3 :(得分:0)
当您尝试检索asynctask
之外的值时,它显示为null,因为asynctask没有完成。无论您在执行asyncTask
时获得的数据需要做什么,都需要在onPostExecute
内完成,这是您可以确定asyncTask
的唯一地方已经完成了。
答案 4 :(得分:0)
提交后重新实例化您的共享偏好。