我正在FaceBook
与Libgdx
. 有些FaceBook
方法需要上下文(来自Android Activity
)。我找到了一些资源来帮助我解决这个问题
在我遵循本教程之后Interface
,我有了我的 .然后我创建了一个实现它的对象Interface
。(此对象包含Context
)并将该对象传递给MainActivity
我的 Android 项目的初始化方法。问题是我无法访问主libgdx
项目中的上下文,因为它无法访问 android 库android.content.Context
。
请帮助我解决此问题,因为必须FaceBook
使用我的应用程序登录。
我已经查看了大量关于此的问题,但没有一个有明确的答案。提前致谢。
//编辑
在尝试了 Vikalp 的解决方案后,我几乎让它工作了。我知道我的 facebook 代码可以正常工作,因为我已经使用原生 android 对其进行了测试。
所以新的解决方案是:在LIBGDX(Maininterface)中创建一个接口,在LIBGDX中创建一个facebookCallBack接口来接收来自MainInterface的回调信息。创建了一个类,该类创建 MainInterface 并包含我所有的 facebook 代码(在本机 android 上按预期工作)。我创建了这个类的一个实例并将其传递给 LIBGDX,在那里我可以从 MainInterface 接收 facebookCallBack 中的回调。
我在 logcat 中收到关于 applicaitonID 的空错误,但是它与我的原生 android 项目的设置相同,因此它应该在清单的元数据中找到 id。
logcat 输出打印如下:
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.<init>(Session.java:221)
07-09 10:32:00.300: E/AndroidRuntime(7261): at com.facebook.Session.<init>(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
//编辑2
在 android 端,我有一个创建 MainInterface 的活动,因为我需要一个活动来启动一个会话。该类的代码如下:
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;
}
}
当按下 LIBGDX 中的按钮时,将调用 getResultFromFaceBook() 方法。
//编辑 3
AppID 在清单中引用如下:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
并且 ID 的值存储在 strings.xml 中,如下所示:
<string name="app_id">**************</string>
packageManager 可能会给出 null 错误,但我不确定。