3

I get an NPE when I try to login to facebook on Kitkat, works fine on Lollipop MR1. I tried 4.0.0 and 4.0.1

My login code:

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList(READ_PERMISSIONS));

the crash is

java.lang.RuntimeException: Unable to start activity ComponentInfo{lt.segfoltas.psm/com.facebook.FacebookActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.facebook.login.LoginFragment.onCreate(LoginFragment.java:68)
            at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1194)
            at android.app.Activity.performStart(Activity.java:5258)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
            at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

The relevant manifest entry

<activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name" />

App id is set inside onCreate

    FacebookSdk.setApplicationId(FacebookData.APP_ID);
    FacebookSdk.sdkInitialize(getApplicationContext());

Immediately after calling login, I get a response that the login was canceled. Then the app crashes.

Facebook's code where the crash is reported:

this.callingPackage = this.getActivity().getCallingActivity().getPackageName();

My permissions:

private static final String[] READ_PERMISSIONS = new String[]{"public_profile", "user_events", "user_birthday", "email"};
DariusL
  • 4,007
  • 5
  • 32
  • 44
  • possible duplicate of [login facebook getlocation and email](http://stackoverflow.com/questions/29605236/login-facebook-getlocation-and-email), check out the answer, I think you will find out what is missing. – Menelaos Kotsollaris Apr 29 '15 at 15:30

2 Answers2

3

Found out what crashed it. Login method crashes inside facebook's LoginFragment if the calling activity is set to singleInstance. I don't know if there's a workaround.

Edit:

It seems like Facebook partially fixed the issue in SDK 4.1

4.0 had:

this.callingPackage = this.getActivity().getCallingActivity().getPackageName();

Which results in a NPE when login is called from a singleInstance activity.

Now it looks like LoginActivity will exit and report an error if this happens.

    // If the calling package is null, this generally means that the callee was started
    // with a launchMode of singleInstance. Unfortunately, Android does not allow a result
    // to be set when the callee is a singleInstance, so we log an error and return.
    if (callingPackage == null) {
        Log.e(TAG, NULL_CALLING_PKG_ERROR_MSG);
        getActivity().finish();
        return;
    }
DariusL
  • 4,007
  • 5
  • 32
  • 44
0

there were changes on how to implement certain features on the Facebook Android SDK, first:

You don't set the applicationId in code, it now can be done though the metadata tag in the AndroidManifest.xml file.

Here is an example:

Right before the closing tag at the manifest you must set:

   <!-- Facebook Integration -->
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

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

Now on your onCreate you use the new LoginManager class for all your login calls that don't require a UI, as the example at the documentation example below:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this.getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().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   
                }
    });
}

You can always see all changes and docs for the new SDK here

UPDATE: Here is an example on how to pass permissions using Array.AsList();

...logInWithReadPermissions(this, Arrays.asList("public_profile", "email");

Joel
  • 838
  • 5
  • 12
  • I'll try moving the application id to meta-data. My callbacks are set as you posted. As I've said, I get a call to onCancel immediately after I call logInWithReadPermissions. – DariusL Apr 29 '15 at 15:24
  • Sure, btw what permissions are you passing via Arrays.asList(READ_PERMISSIONS)) ????, I'll update my answer with what I do in here and please let me know. Thanks – Joel Apr 29 '15 at 15:26