我需要为我的 Android 应用程序实现 facebook 登录。我正在使用andengine,所以我无法使用facebook提供的登录按钮。我还需要用户的电子邮件地址。
为此,我创建了一个名为 facebookActivity 的活动:
public class FacebookActivity extends BaseGameActivity{
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 720;
private Scene scene;
private StatusCallback callback;
@SuppressLint("NewApi")
@Override
public EngineOptions onCreateEngineOptions() {
int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT>8){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
pOnCreateResourcesCallback.onCreateResourcesFinished();
// TODO Auto-generated method stub
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
scene = new Scene();
scene.setBackground(new Background(HelperMethods.getColorFromHex("#372108")));
pOnCreateSceneCallback.onCreateSceneFinished(scene);
}
@Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
callback = new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Log.d("text","Hello " + user.getName() + " " + user.getInnerJSONObject());
}
}
});
}
else{
}
}
};
final OpenRequest openRequest = new OpenRequest(this);
List<String> readPermissions = new ArrayList<String>();
readPermissions.add("email");
openRequest.setPermissions(readPermissions);
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
openRequest.setCallback(callback);
runOnUiThread(new Runnable(){
@Override
public void run() {
new Session(FacebookActivity.this).openForRead(openRequest);
}
});
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
当我运行此代码时,出现异常NullPointerException
:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.vocabtrainer.code/com.vocabtrainer.code.FacebookActivity}: java.lang.NullPointerException
我尝试调试,发现里面 onActivityResult Session.getActiveSession() 为空,但我的应用程序实际上去 facebook 授权我并请求权限。
我错过了什么吗?