我遵循了Facebook SDK for Android 入门教程,并且“if (session.isOpened())”语句从未通过。我的主要活动 java 代码是本教程的精确副本。
我阅读了 hashkey 可能不正确的原因,因此我添加了教程第 4 步的故障排除部分的代码,并将 logcat hashkey 复制到我的 Facebook Dev 帐户,但这没有任何区别。
-我的项目目标:Android 4.3 (18)
-facebook-android-sdk-3.0.2
-java 1.7
-在模拟器中测试
有什么我应该检查的吗?
package com.firstandroidapp;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import com.facebook.*;
import com.facebook.model.*;
import android.widget.TextView;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Log.d("CHECKPOINT", "Session opened!");
// 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) {
TextView welcome = (TextView) findViewById(R.id.welcome);
welcome.setText("Hello " + user.getName() + "!");
}
}
});
}
else
{
Log.d("CHECKPOINT", "No open session");
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}