Search code examples
libgdxaddthis

Addthis.shareItem() in libgdx project throwing NullPointerException


I have a libgdx project where I want to add a sharing button. I want the UI element to be a libgdx one, while the sharing logic to be handled by the AddThis library.

My java code (libgdx UI) is the following:

Button fb = createButton(50, 200, 0, "facebook.png"); //  
stage.addActor(fb);

fb.addListener(new ClickListener()
{

  @Override
  public void clicked(InputEvent event, float x, float y)
  {
     super.clicked(event, x, y);
     getApplicationListener().getNativeMethods().shareFb();
  }
}

I avoid pasting here createButton(), getApplicationListener() and getNativeMethods() because I think their names alone make it clear what they do. Here is the shareFb() method:

public void shareFb()
{
  handler.post(new Runnable()
  { 
    @Override
    public void run()
    {
      try 
      {
        InputStream istr = context.getAssets().open("achievement1.png");
        Bitmap imageBitmap = BitmapFactory.decodeStream(istr);
        AddThis.shareItem(context, "facebook", "Achievement",  "Completed level 1", imageBitmap);
      } 
      catch (Exception e) 
      {
        System.err.println(e.getMessage());
      }      
    }  
  });
} 

However when I click on my UI button, the shareItem() method throws a NullPointerException, here is the relevat part of logcat:

09-23 17:29:37.912: E/AndroidRuntime(9491): Caused by: java.lang.NullPointerException
09-23 17:29:37.912: E/AndroidRuntime(9491):     at com.addthis.core.sharer.ATFacebook.share(ATFacebook.java:53)
09-23 17:29:37.912: E/AndroidRuntime(9491):     at com.addthis.ui.activities.ATShareActivity.startSharing(ATShareActivity.java:130)
09-23 17:29:37.912: E/AndroidRuntime(9491):     at com.addthis.ui.activities.ATShareActivity.onCreate(ATShareActivity.java:74)
09-23 17:29:37.912: E/AndroidRuntime(9491):     at android.app.Activity.performCreate(Activity.java:5451)
09-23 17:29:37.912: E/AndroidRuntime(9491):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
09-23 17:29:37.912: E/AndroidRuntime(9491):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)

The AddThisDemo project, bundled with the AddThis library, does work, and I copied parts of its code, but that's not libgdx, so it's quite distant from what I need. Any clue?


Solution

  • I only forgot to call

     Config.configObject().setFacebookAppId("<my fb app ID>");
    

    EDIT: After adding this line of code in my application initialization, the shareItem() method started working as expected.