我想在我的应用程序中从facebook获取电子邮件和用户名,所以我已经在我的片段中编写了必要的代码。单击“登录”按钮后,它将获取数据并要求获取从Facebook获取数据的权限。好之后它会抛出一个异常
Error java.lang.UnsupportedOperationException: Session: an attempt was made to open a session that has a pending request.
我正在使用的代码是(我尝试过使用onActivityResult())
public class LoginFragment extends Fragment {
LoginButton loginWithFBButton;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_login, container, false);
loginWithFBButton = (LoginButton)v.findViewById(R.id.authButton);
loginWithFBButton.setOnErrorListener(new OnErrorListener(){
@Override
public void onError(FacebookException error) {
Log.i(TAG, "Error " + error.getMessage());
}
});
// set permission list, Don't forget to add email
loginWithFBButton.setReadPermissions(Arrays.asList("basic_info","email"));
// session state call back event
loginWithFBButton.setSessionStatusCallback(new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
Log.i(TAG,"Access Token"+ session.getAccessToken());
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
// TODO Auto-generated method stub
Log.i(TAG,"User ID "+ user.getId());
Log.i(TAG,"Email "+ user.asMap().get("email"));
}
});
}
else if (state.isClosed()) {
}
}
});
return v;
}
public void startActivityForResult(int requestCode, int resultCode, Intent data) {
Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
}
答案 0 :(得分:0)
试试这个:
更改为:
public void startActivityForResult(int requestCode, int resultCode, Intent data) {
Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
到
protected void onActivityResult(int requestCode, int resultCode, Intent data){
Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}