Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook SDK Android AppEventsLogger NullPointerException on flush

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.

like image 566
user1827010 Avatar asked Sep 20 '13 13:09

user1827010


2 Answers

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.

like image 74
Maurice Gavin Avatar answered Oct 30 '22 00:10

Maurice Gavin


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

like image 26
luna Avatar answered Oct 30 '22 00:10

luna