1

I am working in Android Studio in a project that does not have any fragments. I keep getting a NullPointerException on loginButton even after assigning it with the line:

            loginButton = (LoginButton) findViewById(R.id.login_button);

The relevant part of MainActivity.java:

public class MainActivity extends ActionBarActivity {
    LoginButton loginButton;
    CallbackManager callbackManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("user_friends");
        loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });
        setContentView(R.layout.activity_main);
        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
            }
            @Override
            public void onCancel() {
                // App code
            }
            @Override
            public void onError(FacebookException exception) {
                // App code
            }
        });
    }

And activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp"
    tools:context=".MainActivity">        

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="30dp" />
</RelativeLayout>

I cannot figure out why loginbutton would still be null after that line and I am not sure how to fix this. If anyone could help me get a project set up with a Facebook login that works with Android Studio, that would be great even if it uses fragments. I have nothing against fragments, but I am new to Android so I decided not to use fragments because I do not fully understand them. I looked through Facebook's example projects, but I could not find a Android Studio project file and kept failing to set up a Project with their individual sample files.

Pat Myron
  • 4,437
  • 2
  • 20
  • 39

4 Answers4

2

Try to move the setContentView() to any place before assign loginButton. Like this:

public class MainActivity extends ActionBarActivity {
    LoginButton loginButton;
    CallbackManager callbackManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //right here
        setContentView(R.layout.activity_main);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("user_friends");
        loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });
        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // App code
        }
        @Override
        public void onCancel() {
            // App code
        }
        @Override
        public void onError(FacebookException exception) {
            // App code
        }
    });
}
Álisson Morais
  • 356
  • 2
  • 19
2

you can also use the following code it works for me, while using Facebook SDK 4.7

facebook login android

Community
  • 1
  • 1
Kushal
  • 795
  • 1
  • 5
  • 23
1

Your views (login button) must be initialized after setContentView(R.layout.activity_main); So you need to move

callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
loginButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

        }
});

after setContentView(R.layout.activity_main);

NOTE: FB SDK should init before setContentView()

Piyush
  • 18,895
  • 5
  • 32
  • 63
0

This will give a null pointer error. So all you need to do is Initialize the facebookSDK first, then setContentView and then set the callBackManager.

AmitSandesara
  • 35
  • 1
  • 7