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

Android应用点击Google登录/注册按钮后黑屏,新手求助

Hey there! As someone who’s struggled through Google Sign-In setup on Android as a newbie, I totally get how frustrating this blank screen issue is. Let’s walk through the most common fixes that’ll likely get you sorted out:

常见排查与解决步骤

1. 检查Google Sign-In客户端的初始化逻辑

Make sure you’re initializing the GoogleSignInClient correctly before setting up your button’s click listener in SignInActivity’s onCreate method. Here’s a correct example:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);

    // Step 1: Configure sign-in options first
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail() // Don't skip requesting necessary scopes like email/profile
            .requestIdToken(getString(R.string.default_web_client_id)) // Only needed if you're using backend authentication
            .build();

    // Step 2: Create the sign-in client instance
    GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    // Step 3: Set up your custom button click
    Button signUpBtn = findViewById(R.id.your_custom_google_btn);
    signUpBtn.setOnClickListener(v -> {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        // For AndroidX, use registerForActivityResult instead of the deprecated startActivityForResult
        startActivityForResult(signInIntent, 1001); // Use a constant for the request code
    });
}

2. 验证AndroidManifest.xml配置

Double-check these critical entries are present:

  • Add the internet permission (required for Google Sign-In to communicate with servers):
<uses-permission android:name="android.permission.INTERNET" />
  • Include the Google Play Services version metadata:
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

Also confirm you’ve placed the google-services.json file in your app’s root directory (not the res folder!) and that your app-level build.gradle has the Google Services plugin applied at the bottom:

apply plugin: 'com.google.gms.google-services'

3. 检查设备/模拟器环境

  • If using an emulator, make sure you’re using an AVD with Google Play Services pre-installed (look for "Google Play" in the AVD Manager’s device list). Emulators without Google Play can’t handle Google Sign-In at all.
  • On physical devices, ensure Google Play Services is up to date—you can check this in the Google Play Store under "My apps & games".

4. 查看Logcat里的隐藏错误

The blank screen is often a sign of a silent crash that doesn’t show an error dialog. Open Android Studio’s Logcat tab, filter by "Error" level, then click your button again. You’ll probably spot an exception like:

  • NullPointerException (if your GoogleSignInClient wasn’t initialized properly)
  • ApiException (misconfigured client ID or missing scopes)
  • GooglePlayServicesNotAvailableException (outdated/missing Play Services)
    Fixing the specific exception from Logcat is usually the fastest path to resolving the issue.

5. 确认Google Cloud Console配置

Even if you followed the official guide, double-check:

  • Your Android app’s SHA-1 fingerprint is correctly added to your Cloud Console project.
  • The google-services.json file you downloaded matches the project and SHA-1 fingerprint you’re using.
  • The Google Sign-In API is enabled in the Cloud Console (it should be enabled by default, but it’s worth verifying).

内容的提问来源于stack exchange,提问作者Aakriti Johar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:34:54