Android项目Facebook登录问题求助:一键登录后无法跳转至数据页面
Hey there! Let's break down the common issues that might be blocking your Facebook login flow from jumping to the next Activity with collected user data. Since you only shared a snippet of your Test class, I’ll walk through the key checkpoints you need to verify step by step:
The most likely culprit is that you’re either not waiting for full user data to load before jumping, or the callback isn’t set up correctly. Here’s how a complete, working callback should look in your Test class:
public class Test extends AppCompatActivity { private static final String TAG = "SocialActivity"; private CallbackManager callbackManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize Facebook SDK first FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); setContentView(R.layout.activity_test); callbackManager = CallbackManager.Factory.create(); LoginButton loginButton = findViewById(R.id.login_button); // Request necessary permissions (email + public profile are common) loginButton.setPermissions(Arrays.asList("email", "public_profile")); loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { // Fetch user details via GraphRequest AFTER login succeeds AccessToken accessToken = loginResult.getAccessToken(); GraphRequest request = GraphRequest.newMeRequest(accessToken, (object, response) -> { try { // Extract user data from the JSON response String userId = object.getString("id"); String userName = object.getString("name"); String userEmail = object.optString("email"); // Use optString to avoid null crashes // Now launch the Result Activity with collected data Intent intent = new Intent(Test.this, Result.class); intent.putExtra("USER_ID", userId); intent.putExtra("USER_NAME", userName); intent.putExtra("USER_EMAIL", userEmail); startActivity(intent); } catch (JSONException e) { Log.e(TAG, "Failed to parse user data", e); } }); // Specify which fields you want to retrieve Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,email"); request.setParameters(parameters); request.executeAsync(); } @Override public void onCancel() { Log.d(TAG, "Login cancelled by user"); } @Override public void onError(FacebookException error) { Log.e(TAG, "Login failed", error); } }); } // Don't forget to handle the callback result in onActivityResult @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } }
Critical note: Never trigger the Activity jump before the GraphRequest completes—you need to wait until you’ve actually fetched the user data first.
Make sure your Result Activity is properly declared in AndroidManifest.xml, and that Facebook’s required entries are present:
<!-- Required for Facebook SDK --> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id" /> <activity android:name="com.facebook.FacebookActivity" android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:label="@string/app_name" /> <!-- Your Result Activity --> <activity android:name=".Result" />
Double-check that you’re correctly pulling the data in your Result class:
public class Result extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); Intent intent = getIntent(); if (intent != null) { String userId = intent.getStringExtra("USER_ID"); String userName = intent.getStringExtra("USER_NAME"); String userEmail = intent.getStringExtra("USER_EMAIL"); // Use the data (e.g., display it in TextViews) Log.d("ResultActivity", "User: " + userName + " | Email: " + userEmail); } } }
- Check Logcat for errors under your
TAG("SocialActivit...")—look for failed data parsing, login errors, or missing permission warnings. - Confirm your Facebook App ID in
strings.xmlmatches the one in the Facebook Developer Console. - Ensure you’ve added your app’s debug/release hash keys to the Facebook Developer Console (missing keys can cause silent login failures).
If you’ve gone through all these steps and still have issues, share the full code for both Test and Result classes, plus any Logcat error messages—that’ll help narrow down the exact problem!
内容的提问来源于stack exchange,提问作者Roshan Zaid

