AKIWEB
AKIWEB

Reputation: 19612

Facebook Authentication is not working on Android Phone

I have made an android application in which I need to login using Facebook and as soon as authentication is successful, I need to go to another intent. So I downloaded Facebook Android SDK from here.

After download and importing into eclipse I extended the SessionLoginExample from that SDK. In my emulator it is working fine for me, I am able to login to Facebook Application using that and also as soon as I am logged in, it is going to another Intent as well.

Problem Statement:-

But the problem arises after I installed the application on the real Android Phone. After installing it on the Android phone, as soon as it open the Facebook login page, after putting my username and password into that, it is not going to another intent and after sometime I am getting an exception in my console as well. And after that every time I get a message Facebook has stopped working and then I get message my Application has forced closed.

One more thing I have noticed is- As soon as I open my application in Android Phone, I always get message in my console saying that Login Failed without even I have entered my username and password in to Facebook page.

Below is the code I am using from SessionLoginExample-

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);

    buttonLoginLogout = (Button) view.findViewById(R.id.buttonLoginLogout);
    textInstructionsOrLink = (TextView) view.findViewById(R.id.instructionsOrLink);

    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

    Session session = Session.getActiveSession();
    if (session == null) {
        if (savedInstanceState != null) {
        session = Session.restoreSession(getActivity(), null, statusCallback,
            savedInstanceState);
        }
        if (session == null) {
        session = new Session(getActivity());
        }
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
        session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        }
    }

    updateView();

    return view;
    }

    @Override
    public void onStart() {
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);
    }

    @Override
    public void onStop() {
    super.onStop();
    Session.getActiveSession().removeCallback(statusCallback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
    }

    private void updateView() {
    Session session = Session.getActiveSession();
    if (session.isOpened()) {

    // After login is successful, I am going to another Intent
        Intent thesisProject = new Intent(getActivity(), ThesisProjectAndroid.class);
        startActivity(thesisProject);

    } else {
        Log.d(TAG_LOGIN_FAILED,
            "There is something wrong with your Facebook account. Please try again.");

        textInstructionsOrLink.setText(R.string.instructions);
        buttonLoginLogout.setText(R.string.login);
        buttonLoginLogout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            onClickLogin();
        }
        });
    }
    }



    private void onClickLogin() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()) {
        session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
    } else {
        Session.openActiveSession(getActivity(), this, true, statusCallback);
    }
    }

    private void onClickLogout() {
    Session session = Session.getActiveSession();
    if (!session.isClosed()) {
        session.closeAndClearTokenInformation();
    }
    }

    private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        updateView();
    }
    }
}

And I get below exception as well-

01-30 11:06:08.400: E/AndroidRuntime(7463): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=0, data=null} to activity {com.facebook.samples.sessionlogin/com.facebook.LoginActivity}: java.lang.NullPointerException

Can anyone help me out with this why it is happening to my Phone?

Upvotes: 1

Views: 2424

Answers (1)

gkee
gkee

Reputation: 771

From my experience, you can solve the Exception you show by checking the resultCode in onActivityResult, before you pass it up to the Session, e.g.:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (Session.getActiveSession() != null && resultCode == RESULT_OK) {
Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
}
}

Can't help you with the original problem though, of your facebook login not working, sorry.

Upvotes: 3

Related Questions