简易登录应用始终返回错误提示而非成功提示问题求助
Hey Neil, let's figure out why your login flow only shows error prompts and never the success one. First, let's look at the code snippets you shared:
RegisterActivity 代码片段
Intent intent=new Intent(getApplicationContext(),LoginActivity.class); intent.putExtra ( "user", username.getText().toString() ); intent.putExtra ( "pass", password.getText().toString() ); Toast.makeText(getApplicationContext(), Submit, Toast.LENGTH_SHORT).show(); startActivity(intent);
LoginActivity 已提供的代码片段
private EditText username,password; private Button ok,clear; //String Array Str...
问题分析
From what I can see, your RegisterActivity correctly passes the registered username and password to LoginActivity via Intent extras. But the LoginActivity code you shared is missing two critical pieces:
- Initialization of your UI elements (EditText and Button) — if you don't link them to your layout IDs, you'll get a NullPointerException when trying to access their values.
- Validation logic to compare input credentials with the registered ones — right now, there's no code checking if the user's login input matches the account they just registered, so your app has no way to trigger the success prompt.
修复步骤
Initialize UI elements in LoginActivity's
onCreatemethod
Make sure you connect your Java objects to the corresponding views in your XML layout:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_login_layout); // 替换成你的登录页布局ID // 初始化控件 username = findViewById(R.id.username_edittext); // 替换成你布局里的用户名输入框ID password = findViewById(R.id.password_edittext); // 替换成你布局里的密码输入框ID ok = findViewById(R.id.login_ok_button); // 替换成你布局里的登录按钮ID clear = findViewById(R.id.login_clear_button); // 替换成你布局里的清空按钮ID }Add login validation logic to the OK button click listener
Retrieve the registered credentials from the Intent, then compare them with the user's input:ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取从RegisterActivity传递过来的注册信息 String registeredUser = getIntent().getStringExtra("user"); String registeredPass = getIntent().getStringExtra("pass"); // 获取当前登录输入的内容(记得trim掉空格,避免意外匹配失败) String inputUser = username.getText().toString().trim(); String inputPass = password.getText().toString().trim(); // 校验账号密码 if (inputUser.equals(registeredUser) && inputPass.equals(registeredPass)) { // 登录成功提示 Toast.makeText(getApplicationContext(), "登录成功!", Toast.LENGTH_SHORT).show(); } else { // 登录失败提示 Toast.makeText(getApplicationContext(), "账号或密码错误,请重试!", Toast.LENGTH_SHORT).show(); } } });Optional: Add clear button functionality
If you want the clear button to reset the input fields, add this:clear.setOnClickListener(v -> { username.setText(""); password.setText(""); });
额外注意事项
- Always use
equals()to compare strings in Java, not==—==checks object references, whileequals()checks the actual string content. - Trimming input with
trim()helps avoid issues where users accidentally add leading/trailing spaces when registering or logging in. - Make sure the layout IDs you use in
findViewById()exactly match the IDs defined in your XML layout file.
内容的提问来源于stack exchange,提问作者neil tan

