public void onClick(View view){
if(view.getId()==R.id.loginId){
String usr= number.getText().toString();
String pwd= passWord.getText().toString();
boolean containsUsr = newUserIds.contains(usr);
if(containsUsr==true)
{
int index = newUserIds.indexOf(usr);
if(pwd.equals(newPws.get(index).toString()))
{
//Toast.makeText(this,"Logging in",Toast.LENGTH_SHORT).show();
Intent loginActivityIntent= new Intent(getApplicationContext(),GeoWorker.class);
startActivity(loginActivityIntent);
}
else
Toast.makeText(this,"Invalid Password",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(this,"Invalid Phone Number",Toast.LENGTH_SHORT).show();
}
如果电话号码错误,将执行else语句。但无法验证密码。输入密码后,应用程序将关闭。消息显示:应用程序停止工作。 在OnCreate方法中:
SharedPreferences sharedPreferences=this.getSharedPreferences("com.example.monica.miniprojectdemo", Context.MODE_PRIVATE);
userIds= new ArrayList<String>();
userIds.add("1234567890");
userIds.add("9876543210");
userIds.add("8822446688");
userIds.add("0504030201");
userIds.add("519121318");
userIds.add("900000249");
pws = new ArrayList<String>();
pws.add("0000");
pws.add("1111");
pws.add("2222");
pws.add("3333");
pws.add("4444");
pws.add("5555");
try {
sharedPreferences.edit().putString("userName",ObjectSerializer.serialize(userIds)).apply();
sharedPreferences.edit().putString("pass",ObjectSerializer.serialize(pws)).apply();
}catch (Exception e){
e.printStackTrace();
}
newUserIds = new ArrayList<String>();
newPws= new ArrayList<String>();
try {
newUserIds = (ArrayList<String>)ObjectSerializer.deserialize(sharedPreferences.getString("userName", ObjectSerializer.serialize(new ArrayList<String>())));
newPws = (ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("password", ObjectSerializer.serialize(new ArrayList<String>())));
}catch (Exception e){
e.printStackTrace();
}
这是我的学校项目。我的时间很少。