You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

登录页跳转ProfileActivity、Toast及ProgressBar失效问题求助

Fixing LoginActivity: Page Navigation, Toast, and ProgressBar Issues

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 ProfileActivity is added to your AndroidManifest.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.this for 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:06:32