登录页跳转ProfileActivity、Toast及ProgressBar失效问题求助
Hey there! Let's tackle those frustrating issues with your login page step by step. From the code snippet you shared, I can spot a few common pitfalls and missing pieces that are causing the problems.
1. First, Fix the Typo in Activity Name
I notice you wrote ProfileActivit (missing the final 'y') when trying to start the activity. That's a tiny but critical mistake—Android can't find the activity class if the name is misspelled. Correct it to:
startActivity(new Intent(this, ProfileActivity.class));
2. Complete the Login Button Click Logic
Your code is missing the core login trigger: handling the login button's click event. Without this, none of the validation, progress bar, toast, or navigation logic will run. Add this inside your onCreate method:
// Get references to your UI elements Button loginButton = findViewById(R.id.btn_login); // Replace with your button's actual ID editTextUsername = findViewById(R.id.editTextUsername); editTextPassword = findViewById(R.id.editTextPassword); progressBar = findViewById(R.id.progressBar); // Set click listener for login button loginButton.setOnClickListener(v -> { // Get input values String phone = editTextUsername.getText().toString().trim(); String password = editTextPassword.getText().toString().trim(); // Basic input validation if (phone.isEmpty()) { Toast.makeText(LoginActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show(); return; } if (password.isEmpty()) { Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show(); return; } // Show progress bar to indicate loading progressBar.setVisibility(View.VISIBLE); // Simulate a login request (replace with your actual API call) // Use Handler for demo—for real apps, use Coroutines, Retrofit, or OkHttp async calls new Handler(Looper.getMainLooper()).postDelayed(() -> { // Assume login is successful // Save login state to SharedPreferences SharedPrefManager.getInstance(LoginActivity.this).saveUser(phone); // Hide progress bar progressBar.setVisibility(View.GONE); // Show success toast Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_SHORT).show(); // Navigate to ProfileActivity startActivity(new Intent(LoginActivity.this, ProfileActivity.class)); finish(); // Close LoginActivity so user can't go back with back button }, 1500); // Simulate 1.5s network delay });
3. Verify SharedPrefManager Implementation
Make sure your SharedPrefManager has correct methods to save and check login state. A working example:
public class SharedPrefManager { private static SharedPrefManager instance; private final SharedPreferences prefs; private static final String PREF_NAME = "login_session"; private static final String KEY_PHONE = "user_phone"; private SharedPrefManager(Context context) { prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); } public static synchronized SharedPrefManager getInstance(Context context) { if (instance == null) { instance = new SharedPrefManager(context.getApplicationContext()); } return instance; } // Save user login state public void saveUser(String phone) { SharedPreferences.Editor editor = prefs.edit(); editor.putString(KEY_PHONE, phone); editor.apply(); } // Check if user is logged in public boolean isLoggedIn() { return prefs.getString(KEY_PHONE, null) != null; } }
4. Check Layout and Manifest Settings
- ProgressBar Initial Visibility: In your
activity_login.xml, set the progress bar to be hidden by default:<ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone"/> - Register ProfileActivity: Ensure
ProfileActivityis added to yourAndroidManifest.xml:<activity android:name=".ProfileActivity"/>
5. Avoid Common Pitfalls
- Don't Block the Main Thread: If you're using a real API call, never run it on the main thread—this will freeze the UI and prevent the progress bar from showing. Use Coroutines, Retrofit's async callbacks, or similar tools instead.
- Context Consistency: Always use
LoginActivity.thisfor toasts and intent creation inside anonymous listeners to avoid context-related bugs.
Once you implement these fixes, your login page should handle navigation, toasts, and the progress bar as expected!
内容的提问来源于stack exchange,提问作者mach2

