Facebook SDK Android AppEventsLogger NullPointerException on flush
Asked Answered
K

2

6

I integrated the current FacebookSDK for Android v3.5 for tracking purposes. You can track app installs by calling com.facebook.AppEventsLogger.activateApp(context, YOUR_APP_ID);

So far no problems, i can see these events are shown on the facebook dashboard. When it comes to track events or purchase events, facebook recomments doing the following:

m_fbAppEventsLogger = com.facebook.AppEventsLogger.newLogger(applicationcontext);
m_fbAppEventsLogger.logPurchase(BigDecimal.valueOf(4.99), Currency.getInstance("USD"));

It seems that the purchase events get flushed immediately, which results in

09-22 15:10:04.680: D/com.facebook.AppEventsLogger(31691): Caught unexpected exception while flushing: java.lang.NullPointerException

If i try to send a custom event, the same error occurs if the facebook SDK decides to flush the events in its queue.

I found nothing for this behavior, so any help would be greatly appreciated.

Kellsie answered 20/9, 2013 at 13:24 Comment(1)
I am facing the same issue. Did you find a solution for thisFlourishing
I
3

I initialise the Facebook SDK differently to the examples given in their documentation.

Facebook recommend that you define meta-data android:name="com.facebook.sdk.ApplicationId in AndroidManifest.xml and hardcode a value for facebook_app_id in strings.xml. If you are using source control these files are usually checked in and, as a general rule, I don't like to commit my keys to remote repositories.

I pull in a string resource, named facebook_app_id, from an external file with my gradle build. I then define the facebook_app_id manually when I initialise the Facebook SDK rather than relying on the library to find it.

Initialise Facebook SDK

Initialise Facebook SDK (enables tracking of install events):

private void initialiseFacebook(Application application) {
        FacebookSdk.sdkInitialize(application);
        AppEventsLogger.activateApp(application, application.getString(R.string.facebook_app_id));
}

Initialise Facebook AppEventsLogger

Subsequently, I wanted to log some Facebook App Events. I could see that my install events were being successfully tracked by Facebook Analytics. However, I each time I attempted to log an event the following error was returned:

Caught unexpected exception while flushing: java.lang.NullPointerException

For me the solution was to again manually define the facebook_app_id in my method call to create an AppEventsLogger. This solved the problem for me.

AppEventsLogger.newLogger(application, application.getString(R.string.facebook_app_id));

Also: Enable SDK Debugging:

I found it very helpful to have more detailed logs from the Facebook SDK while I was troubleshooting this problem. You can configure it with the code below:

FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.REQUESTS);

This method also has the advantage of letting you use different a facebook_app_id for each build type and/or flavour.

Ier answered 25/1, 2017 at 16:33 Comment(0)
M
2

you should add <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> to your AndroidManifest.xml

Mucker answered 4/11, 2013 at 12:47 Comment(2)
That does not remove the NPE for me.Prowler
Which Facebook-Android-SDK-Version are you using? I have also a constant with the same id here: AppEventsLogger.newLogger(getApplicationContext(), Consts.FACEBOOK_APP_ID);Mucker

© 2022 - 2024 — McMap. All rights reserved.