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.