I want to save user entered login values to save in sharedpreferences
and retrieve it in another page. But the problem is when I go to data retrieving page app is crashing. Please help me.
LoginActivity.java
SharedPreferences loginData = getSharedPreferences("userInfo",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = loginData.edit();
editor.putString("password", passwordbox.getText().toString());
editor.putString("userName", usernamebox.getText().toString());
editor.apply();
Data Retrieving page
public class messagewebview extends AppCompatActivity {
TextView testing_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messagewebview);
SharedPreferences loginData = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
String name = loginData.getString("userName", "");
String pw = loginData.getString("password","");
String msg = "Saved User Name: " + name + "\nSaved Password: " + pw;
testing_name.setText(msg);
}
}
testing_name
未初始化。
Your variable "testing_name" is not initialized. A variable must be initialized before you start using it.
Do following changes in your code.
public class messagewebview extends AppCompatActivity {
TextView testing_name;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messagewebview);
// Provide a appropriate view id
testing_name = findViewById(R.id.testing_name);
SharedPreferences loginData = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
String name = loginData.getString("userName", "");
String pw = loginData.getString("password","");
String msg = "Saved User Name: " + name + "\nSaved Password: " + pw;
testing_name.setText(msg);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.