gpt4 book ai didi

java - 从 asynctask 检索共享首选项值

转载 作者:行者123 更新时间:2023-12-02 03:56:16 28 4
gpt4 key购买 nike

我试图从共享首选项中获取值,但它返回 null。共享首选项值是从 asynctask 设置的。我从 asynctask 内部测试了它,在 toast 中打印 stord 值并且它有效,但问题是当我尝试通过单击按钮注册来检索 asynctask 外部的值时,它显示 null。 logcat 中没有显示错误。

Activity 代码:

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);

}
}

最佳答案

您的问题在于调用 getSharedPreferences(String, int) -

您基本上使用两个单独的 SharePreference 实例,因为您每次都为 SharedPreferences 传递不同的名称(字符串) -

当您存储该值时,您正在使用getSharedPreferences("MyPreferences", context2.MODE_PRIVATE);

当您尝试获取该值时,您正在使用getSharedPreferences("MyPrefs", getApplicationContext().MODE_PRIVATE);

修改您的代码以使用相同的 SharedPreference 名称,它应该可以工作。

关于java - 从 asynctask 检索共享首选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35417697/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com