1

I have been trying to get a Facebook Login (alongside G+ log in) working on my app. G+ works fine, and now I've decided to add Facebook integration.

I have the app ID from facebook, and have provided that in the manifest as so:

<meta-data 
        android:name="com.facebook.skd.ApplicationId"
        android:value="@string/app_id" />

Here is the string resource:

<string name="app_id">000000000000000</string>

And here is my login activity code, with censors on the appropriate information. I do not understand why there is an NullPointerException on the Session.openActiveSessionline in the Facebook section, stating that "Argument 'applicationId' cannot be null. This link will provide you with the LogCat info. I am extremely new to Android development and even more new to working with Facebook, just FYI.

package com.example.example;



import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.android.Facebook;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.model.people.Person;


public class LoginActivity extends Activity implements View.OnClickListener, ConnectionCallbacks, OnConnectionFailedListener{



public static final String PREFS_NAME = "MyPrefsFile";
public static Boolean mFirstTimeUsingApp = true;
private static final String TAG = LoginActivity.class.getSimpleName();

//Google Plus Member Variables
protected static final int REQUEST_CODE_RESOLVE_ERR = 9000;
protected PlusClient mPlusClient;
protected ConnectionResult mPlusConnectionResult;
protected ProgressDialog mPlusConnectionProgressDialog;
protected int SIZE_WIDE = 1;

//Facebook Member Variables
private static final String APP_ID = "000000000000000";
private Facebook mFacebook;
private ProgressDialog mFacebookProgress;


@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    SharedPreferences mSharedPreferences = getSharedPreferences(PREFS_NAME, 0);
    boolean firstTimeUsingapp = mSharedPreferences.getBoolean("firstTimeUsingApp", true);


    mPlusClient = new PlusClient.Builder(this, this, this).setVisibleActivities("http://schemas.google.com/AddActivity").build();

    if (firstTimeUsingapp == true) {

        setContentView(R.layout.activity_login);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);



/*-------------------------------------*/           
/*----------Google Plus Login----------*/
/*-------------------------------------*/           

        SignInButton btnSignInGooglePlus = (SignInButton) findViewById(R.id.google_plus_sign_in_button);
        btnSignInGooglePlus.setSize(SIZE_WIDE);

        findViewById(R.id.google_plus_sign_in_button).setOnClickListener(this);
        //Button btnSignOutGooglePlus = (Button) findViewById(R.id.sign_out);
        Log.v(TAG, "5");

        Log.v(TAG, "6");

        //mPlusClient = new PlusClient.Builder(this, this, this).setVisibleActivities("http://schemas.google.com/AddActivity").build();

        //Progress bar to be displayed if the connection failure is not resolved.
        mPlusConnectionProgressDialog = new ProgressDialog(this);
        mPlusConnectionProgressDialog.setMessage("Signing in...");



/*-------------------------------------*/           
/*-----------Facebook Login------------*/
/*-------------------------------------*/

        mFacebookProgress = new ProgressDialog(this);

        LoginButton mFacebookButton = (LoginButton) findViewById(R.id.facebook_sign_in_button);

        Session.openActiveSession(this, true, new Session.StatusCallback() {

            @Override
            public void call(Session session, SessionState state, Exception exception) {
                if (session.isOpened()) {
                    Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                        @Override
                        public void onCompleted(GraphUser user, Response response) {
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                        }
                    });
                }
            }
        });



/*-------------------------------------*/
/*------------Twitter Login------------*/
/*-------------------------------------*/
//TODO  

/*-------------------------------------*/
/*----------Site Account Login---------*/
/*-------------------------------------*/               
//TODO      
    }
    else {
        setContentView(R.layout.activity_login_pending);
        Log.v(TAG, "99");
    }

}

@Override
protected void onStart() {
    Log.v(TAG, "onStart called");
    super.onStart();
    Log.v(TAG, "super.onStart called");
    if (mPlusClient.isConnected() == false) {
        mPlusClient.connect();
    }
    Log.v(TAG, "mPlusClient connected");
}

@Override
protected void onStop() {
    super.onStop();
    mPlusClient.disconnect();
}


@Override
public void onConnectionFailed(ConnectionResult result) {
    if(mPlusConnectionProgressDialog.isShowing()) {

        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e){
                mPlusClient.connect();
            }
        }
    }

    mPlusConnectionResult = result;
}

@Override
public void onConnected(Bundle arg0) {
    if (mPlusConnectionProgressDialog != null) {
        if (mPlusConnectionProgressDialog.isShowing() == true) {
            mPlusConnectionProgressDialog.dismiss();
        }
    }

    Person person = mPlusClient.getCurrentPerson();

    String userID = person.getId();     

    SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString("userID", userID);
    editor.commit();

    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);

    finish();
}

@Override
public void onDisconnected() {
}

@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
        mPlusConnectionResult = null;
        mPlusClient.connect();
    }
}

@Override
public void onClick(View view) {
    if (view.getId() == R.id.google_plus_sign_in_button && !mPlusClient.isConnected()) {
        if (mPlusConnectionResult == null) {
            mPlusConnectionProgressDialog.show();
        } else {
            try {
                mPlusConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                //Try connecting again.
                mPlusConnectionResult = null;
                mPlusClient.connect();
                mPlusClient.disconnect();
            }
        }
    } else if (view.getId() == R.id.facebook_sign_in_button)  /* && not sure what goes here TODO*/ {

    }

}

protected void errorLogin() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.error_login_title);
    builder.setMessage(R.string.error_login_message);
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}



}
10
  • Are you really using this <string name="app_id">000000000000000</string> String resource ? Or you have posted it here like this, to hide your Real App id ? Oct 13, 2013 at 15:19
  • @SalmanKhakwani I am hiding it.
    – mattcoker
    Oct 13, 2013 at 15:30
  • You have implemented your own Login Activity ? Please explain the working of Login Activity. Oct 13, 2013 at 15:47
  • Main activity checks to see if it is someone's first time opening the app by checking the SharedPreferences. If no value exists, which it shouldn't if it is the first time, defaults to true. Starts intent to start login activity, which will have buttons to login for G+, FB, Twitter, and general site login. Upon logging in and changing the "first time opening app" value of shared preferences, starts main activity, and from then on always opens main. Logout will be a selection only available under settings, accessible from the main (and daughter of main) activities.
    – mattcoker
    Oct 13, 2013 at 15:49
  • Okay, but Facebook SDK maintains cookies already in Shared Prefs. You can simply get the stored cookies by using this method session.getAccessToken() after creating a session object. Oct 13, 2013 at 15:52

1 Answer 1

0

Instead of using a normal button for login/logout purposes, you can use Facebook SDK's own Login/Logout Widget. You can find the implementation details in the following link:
Post to facebook after login fails Android

EDIT:
After initializing the mFacebookButton, set these permissions:

mFacebookButton.setPublishPermissions(Arrays.asList("publish_stream","read_stream"));

Use the code below after setting the permissions correctly.

mFacebookButton.setSessionStatusCallback(new Session.StatusCallback() 
{
public void call(Session session, SessionState state,Exception exception) 
{
// TODO Auto-generated method stub
if (session.isOpened()) 
 {
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {

public void onCompleted(GraphUser user,Response response) 
    {
    // TODO Auto-generated method stub
    if (user != null) 
        {
        Log.i(TAG, "User ID " + user.getId());
        Log.i(TAG,"Email "+ user.asMap().get("email"));
        }
    }});
 } 
else 
 {
    //This else condition will be executed when logout will be clicked.
 }
}
});

Instead of this code:

Session.openActiveSession(this, true, new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                });
            }
        }
    });


I hope this helps.

3
  • Here is my XML for the facebook button. In my Login Activity, the LoginButton type is part of the facebook SDK. <com.facebook.widget.LoginButton android:id="@+id/facebook_sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:layout_marginBottom="30dp" />
    – mattcoker
    Oct 13, 2013 at 15:28
  • I will be changing the code this evening, something just came up. But I thought I would let you know that I don't need reading or publishing abilities at this time, just purely doing a login ability.
    – mattcoker
    Oct 13, 2013 at 15:52
  • You can avoid using the set permissions line. The code should work fine :) Oct 13, 2013 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.