我正在为我的应用添加Facebook共享功能。我使用的是SDK v4.5。在SDK文档中,我们被告知要将以下元数据标记添加到AndroidManifest:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
问题是,我通过发出http请求从服务器获取我的app id。在旧版本的Facebook SDK中,我可以使用java代码中的app id初始化SDK,因此不会出现问题:
mFacebook = new Facebook(FACEBOOK_APP_ID);
但是现在使用新的SDK,如果我没有放置该元数据标记,我会收到以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yrazlik.facebooklogindemo, PID: 6450
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yrazlik.facebooklogindemo/com.yrazlik.facebooklogindemo.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:772)
at com.facebook.internal.Utility.getDialogFeatureConfig(Utility.java:882)
at com.facebook.internal.DialogPresenter.getVersionSpecForFeature(DialogPresenter.java:253)
at com.facebook.internal.DialogPresenter.getProtocolVersionForNativeDialog(DialogPresenter.java:239)
at com.facebook.internal.DialogPresenter.canPresentNativeDialogWithFeature(DialogPresenter.java:73)
at com.facebook.share.widget.ShareDialog.canShowNative(ShareDialog.java:131)
at com.facebook.share.widget.ShareDialog.access$300(ShareDialog.java:53)
at com.facebook.share.widget.ShareDialog$NativeHandler.canShow(ShareDialog.java:239)
at com.facebook.share.widget.ShareDialog$NativeHandler.canShow(ShareDialog.java:231)
at com.facebook.internal.FacebookDialogBase.createAppCallForMode(FacebookDialogBase.java:184)
at com.facebook.internal.FacebookDialogBase.showImpl(FacebookDialogBase.java:147)
at com.facebook.internal.FacebookDialogBase.show(FacebookDialogBase.java:142)
at com.yrazlik.facebooklogindemo.MainActivity.onCreate(MainActivity.java:136)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
那么,我能做些什么吗?我可以以某种方式从java代码设置app id而不是使用元数据标签吗?
感谢。
答案 0 :(得分:0)
我找到了一种方法来做到这一点。显然有一种方法可以从代码中设置app id,而不是使用元数据标记。我从清单中删除了元数据标记,并使用了以下方法,现在它可以正常工作。
FacebookSdk.setApplicationId(AppConstants.FACEBOOK_APP_ID);
FacebookSdk.sdkInitialize(getApplicationContext());
答案 1 :(得分:0)
您可以在启动画面时为facebook app id进行服务调用。并将其保存在共享偏好中。
然后初始化
FacebookSdk.setApplicationId(GET FACEBOOK APP ID FROM SHARED PREFERENCES);
Facebook.sdkInitialize(getApplicationContext());
但请确保您必须首先从服务器获取Facebook应用ID,然后才能初始化。否则申请将被强行关闭。