i am quite new into android coding and have been trying to practice on it. Recently i am trying to create a fuel efficiency calculator with a login and register function. When i tried clicking on the login button it crashes.
import androidx.appcompat.app.AppCompatActivity;
public class Login1 extends AppCompatActivity {
Button bLogin, bRegister;
EditText etUsername, etPassword;
SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
etUsername = (EditText) findViewById(R.id.editText2);
etPassword = (EditText) findViewById(R.id.editText);
bLogin = (Button) findViewById(R.id.button);
bRegister = (Button) findViewById(R.id.button2);
preferences = getSharedPreferences("Userinfo", 0);
bRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Login1.this, Register1.class);
startActivity(intent);
}
});
bLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String usernameValue = etUsername.getText().toString();
String passwordValue =etPassword.getText().toString();
String registeredUsername = preferences.getString("etUsername", "");
String registeredPassword = preferences.getString("etPassword", "");
if (usernameValue.equals(registeredUsername) && passwordValue.equals(registeredPassword)) {
Intent intent = new Intent(Login1.this, MainActivity.class);
startActivity(intent);
}
else {
Toast t = Toast.makeText(Login1.this, "Wrong email or password", Toast.LENGTH_SHORT);
t.show();
}
}
});
}
Previously i had the same issue with register button but i happened to fix it by changing the intent name from i to intent. However this didn't work with my login button. Appreciate any assistance