在我的应用程序中登录 Facebook
后,获取 Profile.getCurrentProfile()
时遇到问题。我正在使用 Facebook Android SDK 4.0.0
。登录后,我想获取用户名并将其显示在 TextView
中,但我得到的是 profile == null
并且我不知道为什么。我有正确的应用 ID 和 HashKey
。
这是我的代码:
public class MyFragment extends Fragment {
CallbackManager callbackManager;
LoginButton loginButton;
FacebookCallback<LoginResult> loginResultFacebookCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Log.e("FB", String.valueOf(accessToken));
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
name.setText("Witam " + profile.getName());
Log.e("FB", "w");
}
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
};
TextView name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myfragment_facebook, container, false);
name = (TextView) view.findViewById(R.id.name);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, loginResultFacebookCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
当我在 Logcat
中检查 Token 时,我得到 AccessToken token:ACCESS_TOKEN_REMOVED
最佳答案
对我来说,代码 Profile profile = Profile.getCurrentProfile();在安装 FB android 应用程序时工作,并且在卸载 fb 应用程序时同样抛出空指针异常,因此在那个实例中我的应用程序调用了 fb web 应用程序(它没有返回配置文件)。这可能是 FB 中的现有错误开发工具包
关于android - Facebook Android SDK 4.0.0 没有获取个人资料信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29603524/