title: facebook integration with libgdx on android
body: <p>I am integrating <code>FaceBook</code> with <code>Libgdx</code>. Some of the <code>FaceBook</code> methods require a context (from the <code>Android Activity</code>). I have found some resources to help me with <a href="https://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements3TierProjectSetup" rel="nofollow">this</a> </p>
  
  <p>After I followed this tutorial I had my <code>Interface</code>, I then created an object that implemented that <code>Interface</code>. (this object contained the <code>Context</code>) and passed that object into the initialize method in the <code>MainActivity</code> of my Android project. The problem is I cannot access the context in my main <code>libgdx</code> project as it cannot access the android libraries <code>android.content.Context</code>. </p>
  
  <p>Please help me resolve this issue as it is necessary to have <code>FaceBook</code> login with my app. </p>
  
  <p>I have already looked through a massive amount of questions on this and not one has a definitive answer. Thanks in advance. </p>
  
  <p>//EDIT</p>
  
  <p>After attempting Vikalp's solution I nearly have it working. I know my facebook code works as I have tested it with native android.</p>
  
  <p>SO the new solution is: Created an interface in LIBGDX(Maininterface), created a facebookCallBack interface in LIBGDX to receive callback info from MainInterface. 
  Created a class which creates the MainInterface and contains all my facebook code(which works as expected on native android). I create an instance of this class and pass it through to LIBGDX where I can receive callbacks in facebookCallBack, from MainInterface. </p>
  
  <p>Im getting a null error in the logcat about the applicaitonID, however it is the same set up as my native android project so it should find the id in the meta data of the manifest.</p>
  
  <p>The logcat output is printed below: </p>
  
  <pre><code>07-09 10:31:57.660: D/dalvikvm(7261): GC_CONCURRENT freed 214K, 7% free 12371K/13191K,              paused 3ms+12ms, total 34ms
  07-09 10:32:00.200: I/System.out(7261): clicked!
  07-09 10:32:00.210: W/dalvikvm(7261): VFY: unable to find class referenced in signature    (Landroid/support/v4/app/Fragment;)
  07-09 10:32:00.210: W/dalvikvm(7261): VFY: unable to find class referenced in signature (Landroid/support/v4/app/Fragment;)
  07-09 10:32:00.210: I/dalvikvm(7261): Could not find method android.support.v4.content.LocalBroadcastManager.getInstance, referenced from method com.facebook.Session.postActiveSessionAction
  07-09 10:32:00.210: W/dalvikvm(7261): VFY: unable to resolve static method 304: Landroid/support/v4/content/LocalBroadcastManager;.getInstance (Landroid/content/Context;)Landroid/support/v4/content/LocalBroadcastManager;
  07-09 10:32:00.210: D/dalvikvm(7261): VFY: replacing opcode 0x71 at 0x0009
  07-09 10:32:00.295: W/dalvikvm(7261): threadid=11: thread exiting with uncaught exception (group=0x40d422a0)
  07-09 10:32:00.300: E/AndroidRuntime(7261): FATAL EXCEPTION: GLThread 2837
  07-09 10:32:00.300: E/AndroidRuntime(7261): java.lang.NullPointerException
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:86)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.facebook.internal.Utility.getMetadataApplicationId(Utility.java:166)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.facebook.Session.&lt;init&gt;(Session.java:221)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.facebook.Session.&lt;init&gt;(Session.java:213)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.facebook.Session$Builder.build(Session.java:1454)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.facebook.Session.openActiveSession(Session.java:863)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.facebook.Session.openActiveSession(Session.java:805)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.FB.GDX.FBActivity$1.getResultFromFaceBook(FBActivity.java:66)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.FB.GDX.GDXScreen$1.clicked(GDXScreen.java:123)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:82)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:57)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:334)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.badlogic.gdx.backends.android.AndroidInput.processEvents(AndroidInput.java:359)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:486)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
  07-09 10:32:00.300: E/AndroidRuntime(7261):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
  07-09 10:32:04.060: D/dalvikvm(7261): GC_CONCURRENT freed 356K, 8% free 12449K/13447K,             paused 3ms+2ms, total 22ms
  </code></pre>
  
  <p>//EDIT 2</p>
  
  <p>On the android side, I have an activity that creates the MainInterface as I need an activity to start a Session. The code of that class is below: </p>
  
  <pre><code>public class FBActivity extends Activity {
  
  
  private Activity thisActivity = this;
  private MainInterface object;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  
  
  }
     //methods
      public MainInterface getMaininterface()
      {
          if(object==null)
          {
              //create MainInterface object for facebook callbacks
              object = new MainInterface(){
  
                  CallBackInterface fbCallbacks;
  
                  private String TAG = "MainActivity";
  
                  public Session.StatusCallback callback;
  
  
  
                  public void registerCallBackInterFace(CallBackInterface callBacks){
  
                      this.fbCallbacks = callBacks;
  
                  }
  
                  public Session.StatusCallback getCallback()
                  {
                      return this.callback;
                  }
  
                  public void getResultFromFaceBook(){
                      /* write your code for faceBook here */
  
  
                      Session.openActiveSession(thisActivity,true, new Session.StatusCallback() {
                          @Override
                          public void call(Session session, SessionState state, Exception exception) {
                              if (state.isOpened()) { //note: I think session.isOpened() is the same
  
                              fbCallbacks.setLoggedIn(true);
  
                                  Log.i(TAG, "Access Token" + session.getAccessToken());
  
                                  Request.executeMeRequestAsync(session,
  
                                  new Request.GraphUserCallback() {
  
  
                                      @Override
                                      public void onCompleted(GraphUser user,
                                              Response response) {
                                          // TODO Auto-generated method stub
                                              if (user != null) {
  
                                              email = user.asMap().get("email").toString();
  
                                              Log.i(TAG, "User ID " + user.getId());
  
                                              Log.i(TAG, "Email " + user.asMap().get("email"));
  
  
  
  
                                          }
                                      }
  
                                  });
  
                              } else if (state.isClosed()) {
  
  
  
                                  fbCallbacks.setLoggedIn(false);
                              }
  
  
                          }
  
  
  
  
                      });
                      System.out.println("main interface created");
  
  
                  }
  
  
                  };}
          return object;
      }
      @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The getResultFromFaceBook() method gets called when a button in LIBGDX is pressed. </p>
  
  <p>//EDIT 3</p>
  
  <p>The AppID is referenced in the manifest like this:</p>
  
  <pre><code>&lt;meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/&gt;
  </code></pre>
  
  <p>and the value of the ID is stored in strings.xml as such:</p>
  
  <pre><code>&lt;string name="app_id"&gt;**************&lt;/string&gt;
  </code></pre>
  
  <p>The packageManager may be giving the null error but I'm not sure.</p>