我遇到了这个问题:当我尝试使用方法“Profile.getprofile()”时 即使使用ProfileTracker,它也会返回null。
这是我的代码:
public class Login extends Activity implements OnClickListener,ConnectionCallbacks, OnConnectionFailedListener {
//TODO Facebook
ProfileTracker mProfileTracker;
private CallbackManager callbackManager;
final List<String> permisos = new ArrayList<String>();
private FacebookCallback<LoginResult> fb = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
mProfileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
}
};
mProfileTracker.startTracking();
AccessToken token = loginResult.getAccessToken();
Profile user = Profile.getCurrentProfile();
String name = user.getName();
Toast.makeText(Login.this, "Connected "+name , Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
};
my onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.init_login);
imgFacebook = (ImageView) findViewById(R.id.login_img_facebook);
imgFacebook.setOnClickListener(this);
permisos.add("user_friends");
permisos.add("public_profile");
LoginManager.getInstance().setLoginBehavior(LoginBehavior.SUPPRESS_SSO);
LoginManager.getInstance().registerCallback(callbackManager, fb);
}
我的onClick:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login_img_facebook:
LoginManager.getInstance().logInWithReadPermissions(this,permisos);
break;
}
}
我的onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
我将此添加到我的清单中:
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
<activity android:name="com.facebook.FacebookActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
顺便说一句,我使用eclipse而不是Android Studio
答案 0 :(得分:0)
错误的代码放置
AccessToken token = loginResult.getAccessToken();
Profile user = Profile.getCurrentProfile();
String name = user.getName();
Toast.makeText(Login.this, "Connected "+name , Toast.LENGTH_LONG).show();
上面的代码应该放在跟踪器的功能
中mProfileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
// Place above code here...
}
};