我是Android和Fb sdk的新手。请发布任何建议或参考。
在我清除数据之前它工作正常。现在应用程序在冷启动时崩溃。我已经检查了登录状态,但仍然与currentAccessToken
的生命周期混淆。我可以遵循accessToken
的任何文档或生命周期图吗?这是我一直在尝试的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
initialiseFacebookSdk();
setContentView(R.layout.activity_signin);
ButterKnife.bind(this);
animShow = AnimationUtils.loadAnimation(this, R.anim.slide_up);
mSignIn.startAnimation(animShow);
mLogoImg.setAlpha(200);
}
public boolean isLoggedIn() {
return AccessToken.getCurrentAccessToken() != null && Profile.getCurrentProfile() != null;
}
private void initialiseFacebookSdk() {
FacebookSdk.sdkInitialize(this.getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
String userId = loginResult.getAccessToken().getUserId();
String accessToken = loginResult.getAccessToken().getToken();
Log.d(TAG, "UserId: " + userId + "\n" + "AccessToken: " + accessToken + "");
// Add code here for sending userId and access token to api
Intent intent = new Intent(SignInActivity.this, HomeScreenActivity.class);
startActivity(intent);
}
@Override
public void onCancel() {
Snackbar.make(getCurrentFocus(), "Login cancelled!", Snackbar.LENGTH_LONG)
.setActionTextColor(ContextCompat.getColor(getApplicationContext(), R.color.snackbar_action_color))
.show();
}
@Override
public void onError(FacebookException error) {
Snackbar.make(getCurrentFocus(), "Login error. Retry!", Snackbar.LENGTH_LONG)
.setActionTextColor(ContextCompat.getColor(getApplicationContext(), R.color.snackbar_action_color))
.show();
Log.d(TAG, error + "");
}
});
}
@OnClick({R.id.sign_fb_splash_linear})
void onClick() {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (isLoggedIn() ) {
Intent intent = new Intent(SignInActivity.this, HomeScreenActivity.class);
startActivity(intent);
} else {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
}
这里是logcat
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.debdeep.partyhere.activity.SignInActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3588)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3631)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183)
at android.support.design.widget.Snackbar.make(Snackbar.java:215)
at com.debdeep.partyhere.activity.SignInActivity$1.onError(SignInActivity.java:101)
at com.facebook.login.LoginManager.finishLogin(LoginManager.java:579)
at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:216)
at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:159)
at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
at com.debdeep.partyhere.activity.SignInActivity.onActivityResult(SignInActivity.java:129)
at android.app.Activity.dispatchActivityResult(Activity.java:6235)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3584)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3631)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
答案 0 :(得分:0)
首先,输入initialiseFacebookSdk();在super.onCreate(savedInstanceState)之后; 它必须是第一线。 其次,“this.getApplicationContext()”可能是一个错误,只需编写getApplicationContext()。
如果您还没有,请查看此链接和guid: https://developers.facebook.com/docs/facebook-login/access-tokens