gpt4 book ai didi

android - android中的共享首选项显示未找到默认值

转载 作者:行者123 更新时间:2023-11-30 01:22:47 25 4
gpt4 key购买 nike

我在存储和获取共享首选项方面很吃惊。当用户注册后,如果注册成功,则该值必须存储在他的个人资料中,除了我的个人资料类。我不知道为什么要获取默认值给我一个建议。

提前致谢。

请再次告诉我一些事情,我在这里更新了代码。

  public class Register extends Activity  {


/**
* Defining layout items.
**/

EditText mFirstName, mLastName, mUsername, mMobile, mPassword;
ImageView img_back;
Button btnRegister, mbtnSignIn;
TextView registerErrorMsg;
String jsonresponce=null;
String enteredUsername,enteredPassword,enteredfname,enteredlname,enteredmobile;
String REGISTER_URL;
ProgressDialog pd;
private SharedPreferences sharedpreferences;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mFirstName = (EditText) findViewById(R.id.fname);
mLastName = (EditText) findViewById(R.id.lname);
mUsername = (EditText) findViewById(R.id.username);
mMobile = (EditText) findViewById(R.id.mobie);
mPassword = (EditText) findViewById(R.id.pword);
btnRegister = (Button) findViewById(R.id.register);
mbtnSignIn = (Button) findViewById(R.id.signin);
registerErrorMsg = (TextView) findViewById(R.id.tv);
sharedpreferences = getSharedPreferences("a2a", Context.MODE_PRIVATE);
img_back = (ImageView) findViewById(R.id.imageback);

img_back.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i_backtohome = new Intent(Register.this, HomeActivity.class);
startActivity(i_backtohome);
}
});
mbtnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i_backtosignin = new Intent(Register.this, Signin.class);
startActivity(i_backtosignin);

}
});


btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enteredUsername = mUsername.getText().toString();
Log.e("username",enteredUsername);

enteredPassword = mPassword.getText().toString();
Log.e("username",enteredPassword);

enteredfname = mFirstName.getText().toString();
Log.e("username",enteredfname);

enteredlname = mLastName.getText().toString();
Log.e("username",enteredlname);

enteredmobile = mMobile.getText().toString();
Log.e("username",enteredmobile);

REGISTER_URL = "http://ffhhhjkhjgh/register.php?UserName="+enteredUsername+"&FirstName="+enteredfname+"&LastName="+enteredlname+"&Mobile="+enteredmobile+"&Pass="+enteredPassword;

// REGISTER_URL = REGISTER_URL + enteredUsername + enteredPassword + enteredfname + enteredlname + enteredmobile;
Log.e("finalurl",REGISTER_URL);

if (enteredUsername.equals("") || enteredfname.equals("") || enteredlname.equals("")
|| enteredmobile.equals("") || enteredPassword.equals("")) {

Toast.makeText(Register.this, "All Fields are Mandatory", Toast.LENGTH_LONG).show();

return;
}
if (mUsername.length() <= 1 || mPassword.length() <= 1) {

Toast.makeText(Register.this, "Username or password length must be greater than one", Toast.LENGTH_LONG).show();

return;

}
new AsyncDataClass().execute();

}
});
}

class AsyncDataClass extends AsyncTask<String, String, String> {

/* String fName;
String lName;
public AsyncDataClass(){
this.fName = enteredfname;
this.lName = enteredlname;
}*/

@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(Register.this);
pd.setMessage("Loading...");
pd.setCanceledOnTouchOutside(false);
pd.show();
}

@Override
protected String doInBackground(String... params) {

try{
URL url = new URL(REGISTER_URL);
String res;

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
res = conn.getResponseMessage();
Log.e("res", res);


InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;

try{
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
in.close();
reader.close();
jsonresponce = sb.toString();
Log.e("jsondata", jsonresponce);
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
conn.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
}

}catch (Exception e){
e.printStackTrace();
}
return jsonresponce;
}

@Override
protected void onPostExecute(String s) {

pd.dismiss();

if("SUCCESS".equals(jsonresponce)){
Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("a2a", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("enteredfname", enteredfname);
editor.putString("enteredlname", enteredlname);
editor.commit();
}else{
Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
}

super.onPostExecute(s);
}
}
}

最佳答案

您正在同时使用 getSharedPreferences 和 getDefaultSharedPreferences。

这样做——

SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("a2a", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("enteredfname", enteredfname);
editor.putString("enteredlname", enteredlname);
editor.commit();

后来——

SharedPreferences preferences = getApplicationContext().getSharedPreferences("a2a", Context.MODE_PRIVATE);
String value = preferences.getString("enteredfname", "enteredfname");

关于android - android中的共享首选项显示未找到默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36856357/

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