这是我的MainActivity类:
package com.example.alon.a2018_17_12_userloginexhomework;
公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {
public static final String USERNAME = "username";
public static final String SP_LOGGED_USER = "spLoggedUser";
public static final String PREFS = "prefs";
public static final int REQUEST_CODE = 123;
public static final String DESTINATION = "destination";
public static final String REGISTERED_USERS_SET = "registeredUsersSet";
EditText etUsername, etPassword;
Button btnLogin, btnSignup;
public static HashMap<String, User> hashMap;
public static SharedPreferences sharedPreferences;
Set<String> registeredUsersSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
checkDestination();
checkForLoggedOrRegisteredUsers();
}
private void checkDestination() {
int destination = getIntent().getIntExtra(DESTINATION, -1);
if (destination == 0)
finish();
}
private void checkForLoggedOrRegisteredUsers() {
String loggedInUser = getSharedPreferences(PREFS, MODE_PRIVATE)
.getString(SP_LOGGED_USER, null);
if (loggedInUser != null){
User user = new User(loggedInUser);
Intent intent = new Intent(this, ActivityUserLoggedIn.class);
intent.putExtra(USERNAME, user.getUsername());
startActivityForResult(intent,REQUEST_CODE);
}
try {
registeredUsersSet = getSharedPreferences(PREFS, MODE_PRIVATE)
.getStringSet(REGISTERED_USERS_SET, null);
}catch (NullPointerException e){
e.printStackTrace();
}
if (registeredUsersSet != null){
for (String s : registeredUsersSet){
User u = new User(s);
hashMap.put(u.getUsername(),u);
}
}
}
private void init() {
registeredUsersSet = new HashSet<>();
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
btnLogin = findViewById(R.id.btnLogin);
btnSignup = findViewById(R.id.btnSignup);
hashMap = new HashMap<>();
sharedPreferences = getSharedPreferences(PREFS,MODE_PRIVATE);
btnLogin.setOnClickListener(this);
btnSignup.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnSignup:
signUp();
break;
case R.id.btnLogin:
login();
break;
}
}
private void login() {
String user = etUsername.getText().toString();
String password = etPassword.getText().toString();
//logic check:
if (user.length() < 1 || password.length() < 1){
Toast.makeText(this,
"user or pass must be above 1 chars",
Toast.LENGTH_SHORT).show();
}
//username check
if (hashMap.containsKey(user)){
//password check:
if (Objects.requireNonNull(hashMap.get(user)).getPassword().equals(password)){
//all checks are okay, logging in:
Intent intent = new Intent(this, ActivityUserLoggedIn.class);
intent.putExtra(USERNAME, user);
sharedPreferences.edit().putString(SP_LOGGED_USER,user).apply();
startActivityForResult(intent, REQUEST_CODE);
} else {
//password error:
Toast.makeText(this,
"password does not match, please retry"
, Toast.LENGTH_SHORT).show();
etPassword.setText("");
}
} else {
//username error:
Toast.makeText(this,
"User does not exists, please try again"
, Toast.LENGTH_SHORT).show();
clearEditTexts();
}
}
private void signUp() {
String user = etUsername.getText().toString();
String password = etPassword.getText().toString();
if (user.length() < 1 || password.length() < 1){
Toast.makeText(this,
"user or pass must be above 1 chars",
Toast.LENGTH_SHORT).show();
} else if (hashMap.containsKey(user)){
Toast.makeText(this,
"user already exists, please choose a different usename",
Toast.LENGTH_SHORT).show();
clearEditTexts();
} else {
User newUser = new User(user,password);
hashMap.put(user,newUser);
registeredUsersSet.add(newUser.toString());
Toast.makeText(this,
"successfully registered used name " + user,
Toast.LENGTH_SHORT).show();
clearEditTexts();
}
}
private void clearEditTexts(){
etUsername.setText("");
etPassword.setText("");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK){
if (data != null) {
int destination = data.getIntExtra(DESTINATION, -1);
if (destination == 0)
finish();
}
}
}
@Override
protected void onStop() {
super.onStop();
sharedPreferences.edit().putStringSet(REGISTERED_USERS_SET, registeredUsersSet).apply();
}
}
每次我单击注册按钮时,都会在HashSet对象上获得NPE。谁能指出原因?看来HashSet对象的初始化早于click方法,所以我真的不知道为什么它会变为null。
我得到的错误:
java.lang.NullPointerException:尝试在空对象引用上调用接口方法'boolean java.util.Set.add(java.lang.Object)' 在com.example.alon.a2018_17_12_userloginexhomework.MainActivity.signUp(MainActivity.java:144)
答案 0 :(得分:0)
在onCreate方法中,您调用:
init()->在这里您初始化您的registeredUsersSet
但不久之后,您致电:
checkForLoggedOrRegisteredUsers()->在这里,您再次初始化了registeredUsersSet,但这一次是从sharedPreferences初始化的,在您的情况下可能返回null。
(registeredUsersSet = getSharedPreferences(PREFS, MODE_PRIVATE)
.getStringSet(REGISTERED_USERS_SET, null);)
您缺少此信息:
if (registeredUsersSet ==null)
registeredUsersSet = new HashSet<>();