问题
When i run my Android app, and click approve to the give permissions it not get redirected to the MainActivity. The "Logged in" message doesn't shows up in the Catlog. I have read the Facebook developers guide, and compared my code to different topics here at Stack. I can't see i have done anything wrong.
I would be very glad for help.
public class Login extends Activity {
/**
* Called when the activity is first created.
*/
private CallbackManager callbackManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_login);
final CallbackManager callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile", "email", "user_friends");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
System.out.print("Logged in");
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
Log.i("Error" , "Error");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
StackTrace
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {org.MYapp.FBtestApp/org.MYapp.FBtestApp.Login}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.facebook.CallbackManager.onActivityResult(int, int, android.content.Intent)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3974)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4017)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.facebook.CallbackManager.onActivityResult(int, int, android.content.Intent)' on a null object reference
at org.MYapp.FBtestApp.Login.onActivityResult(Login.java:98)
回答1:
For the login to work, you have to remove the onActivityResult()
method from inside the FacebookCallback
anomymous class.
The correct code should look like this:
private CallbackManager callbackManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile", "email", "user_friends");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
System.out.print("Logged in");
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
Log.i("Error" , "Error");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
回答2:
First of all, insert private private CallbackManager callbackManager;
into your code. It should look something like this:
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
Then add FacebookSdk.sdkInitialize(getApplicationContext());
before super.onCreate(savedInstanceState);
Try this Hope this works
回答3:
The following worked for me:
Added:
FacebookSdk.sdkInitialize(getApplicationContext());
Before:
super.onCreate(savedInstanceState);
It was strange, for everything was working fine earlier; but suddenly, I had this issue. Now, it's fixed with the aforementioned solution.
来源:https://stackoverflow.com/questions/30527073/login-button-facebook-android-doesnt-redirect-to-new-activity