小能豆

Android - Login page is coming as blank

javascript

When I try to load the app, instead of the login page, I see just a blank page with nothing. Can anyone help me why its happening? I don’t see any error, logs or anything that can help me dig deep. Code is compiling fine but its just not showing me the page.

Here is all the code for your reference -


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:roundIcon="@drawable/logo"
        android:supportsRtl="true"
        android:theme="@style/Theme.Bakarstation"
        android:usesCleartextTraffic="true"
        tools:targetApi="31">
        <activity
            android:name=".SplashActivity"
            android:exported="false" />
        <!-- <activity -->
        <!-- android:name=".PostsFeedActivity" -->
        <!-- android:exported="false" /> -->
        <!-- Need to remove above setting in prod env -->
        <activity
            android:name=".MainActivity"
            android:exported="false" />
        <activity
            android:name=".LoginActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

public class LoginActivity extends AppCompatActivity {

    private SignInClient oneTapClient;
    private BeginSignInRequest signUpRequest;
    ImageButton googleSignInButton;

    private static final int REQ_ONE_TAP = 2;  // Can be any integer unique to the Activity.
    private final boolean showOneTapUI = true;
    String userName;
    String userDisplayName;

    @Override
    public void onCreate(Bundle savedInstanceState,
                         PersistableBundle persistentState) {

        super.onCreate(savedInstanceState, persistentState);
        Log.i("CUSTOM MESSAGE", "Hello from Login Activity");

        googleSignInButton = findViewById(R.id.google_login_button);

        oneTapClient = Identity.getSignInClient(this);
        signUpRequest = BeginSignInRequest.builder()
                .setGoogleIdTokenRequestOptions(BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
                        .setSupported(true)
                        // Your server's client ID, not your Android client ID.
                        .setServerClientId(getString(R.string.web_client_id))
                        // Show all accounts on the device.
                        .setFilterByAuthorizedAccounts(false)
                        .build())
                .build();

        ActivityResultLauncher<IntentSenderRequest> activityResultLauncher =
                registerForActivityResult(new ActivityResultContracts.StartIntentSenderForResult(), new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getResultCode() == Activity.RESULT_OK) {
                            try {
                                SignInCredential credential = oneTapClient.getSignInCredentialFromIntent(result.getData());
                                String idToken = credential.getGoogleIdToken();
                                if (idToken != null) {
                                    // Got an ID token from Google. Use it to authenticate
                                    // with your backend.

                                    //We can authenticate with our own server here as this will help establish that the user exists in our DB
                                    userName = credential.getId();   //userName will be google email id
                                    //Make a call to our backend server to make sure that the user exists and pull his id and alias name
                                    userDisplayName = credential.getDisplayName();
                                    boolean userExists = false;
                                    Toast.makeText(LoginActivity.this, "email:" + userName, Toast.LENGTH_SHORT).show();


                                    Log.d("TAG", "Got ID token.");
                                }
                            } catch (ApiException e) {
                                //Change following with logging
                                e.printStackTrace();
                            }
                        }
                    }
                });

        googleSignInButton.setOnClickListener(v -> oneTapClient.beginSignIn(signUpRequest)
                .addOnSuccessListener(LoginActivity.this, result -> {
                    IntentSenderRequest intentSenderRequest = new IntentSenderRequest.Builder(result.getPendingIntent().getIntentSender()).build();
                    activityResultLauncher.launch(intentSenderRequest);
                })
                .addOnFailureListener(LoginActivity.this, e -> {
                    // No Google Accounts found. Just continue presenting the signed-out UI.
                    Log.d("TAG", e.getLocalizedMessage());
                }));
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:background="@color/white"
    tools:context=".LoginActivity">
    <ImageButton
        android:id="@+id/google_login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:contentDescription="@string/login_with_google"
        android:src="@drawable/android_dark_sq_ctn"
        android:tag="google_login_button"
        android:text="@string/sign_in_google" />
</RelativeLayout>

I explained in the problem that the page is not loading and there is no error or logs that I could see.


阅读 107

收藏
2023-11-21

共1个答案

小能豆

It seems like your LoginActivity may not be getting launched properly. I noticed that you are using the ImageButton googleSignInButton = findViewById(R.id.google_login_button); before calling setContentView(R.layout.your_layout); in your onCreate method. This could lead to a NullPointerException as the view hasn’t been inflated yet.

Try modifying your onCreate method like this:

public class LoginActivity extends AppCompatActivity {

    private SignInClient oneTapClient;
    private BeginSignInRequest signUpRequest;
    ImageButton googleSignInButton;

    private static final int REQ_ONE_TAP = 2;  // Can be any integer unique to the Activity.
    private final boolean showOneTapUI = true;
    String userName;
    String userDisplayName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout); // Replace 'your_layout' with the actual layout name

        googleSignInButton = findViewById(R.id.google_login_button);

        // ... rest of your code ...
    }

    // ... rest of your code ...
}

Make sure to replace your_layout with the actual layout file name that contains your google_login_button ImageButton.

If the problem persists, you might want to check the following:

  1. Ensure that the LoginActivity is declared in your AndroidManifest.xml file.
  2. Make sure that there are no issues with the layout file (your_layout.xml) and that it’s correctly referencing the ImageButton.

If the issue still persists, you might want to check the logcat output in Android Studio for any runtime errors or warnings. This could give you more insights into what might be going wrong during the activity’s lifecycle.

2023-11-21