Facebook SDK 4.0.0,Android上的ShareDialog始终为null

时间:2015-04-06 04:50:42

标签: java android facebook android-studio

我想使用其SDK在Facebook上分享一些东西。我的Facebook引用Share Dialog基于Facebook SDK 4.0.0。

    CallbackManager callbackManager;
        ShareDialog shareDialog;

        @Override
        protected void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            this.getWindow().addFlags(Window.FEATURE_NO_TITLE);
            this.setContentView(R.layout.v3_activity_splash);

            // Init Facebook SDK
            FacebookSdk.sdkInitialize(getApplicationContext());
            callbackManager = CallbackManager.Factory.create();
            shareDialog = new ShareDialog(this);
            shareOnFacebook("This is test...");

            ...
        }

    private void shareOnFacebook(String content)
    {
        if (ShareDialog.canShow(ShareLinkContent.class))
        {
            Log.e("Test", "inside shareOnFacebook()");

            //            ShareLinkContent shareLinkContent = new ShareLinkContent.Builder().setContentDescription(content).build();
            ShareLinkContent linkContent = new ShareLinkContent.Builder()
                    .setContentTitle("Hello Facebook")
                    .setContentDescription(content)
                    .setContentUrl(Uri.parse("http://developers.facebook.com/android"))
                    .build();

            shareDialog.show(linkContent);
        }
        else
        {
            Toast.makeText(this, "NOT CALLED", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);

        Log.e("Test", "Yes, Uuu bitch!!!");
    }

我在Application标记中的清单文件中还有以下代码行:

<meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="MY_APP_ID"/>

        <provider
            android:name="com.facebook.FacebookContentProvider"
          android:authorities="com.facebook.app.FacebookContentProviderMY_APP_ID"
            android:exported="true"/>

当我在shareDialog.show(linkContent);行启动应用/活动时,我的问题是NullPointerException。然而,有趣的是shareDialog根据我的调试不是空的:(所以不知道为什么&#34; NullPointerException&#34;抛出。我也没有看到onActivityResult的日志因此不会调用此函数。

enter image description here

最后日志显示:

Process: MY_PCKAGE_NAME, PID: 2152
    java.lang.RuntimeException: Unable to start activity ComponentInfo{MY_PCKAGE_NAME/MY_PCKAGE_NAME.v3.splash.SplashActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2281)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5230)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:780)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:596)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:883)
            at com.facebook.internal.Utility.getDialogFeatureConfig(Utility.java:859)
            at com.facebook.internal.DialogPresenter.getVersionSpecForFeature(DialogPresenter.java:247)
            at com.facebook.internal.DialogPresenter.getProtocolVersionForNativeDialog(DialogPresenter.java:234)
            at com.facebook.internal.DialogPresenter.canPresentNativeDialogWithFeature(DialogPresenter.java:75)
            at com.facebook.share.widget.ShareDialog.canShowNative(ShareDialog.java:133)
            at com.facebook.share.widget.ShareDialog.access$300(ShareDialog.java:55)
            at com.facebook.share.widget.ShareDialog$NativeHandler.canShow(ShareDialog.java:241)
            at com.facebook.share.widget.ShareDialog$NativeHandler.canShow(ShareDialog.java:233)
            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 MY_PCKAGE_NAME.splash.SplashActivity.shareOnFacebook(SplashActivity.java:414)
            at MY_PCKAGE_NAME.splash.SplashActivity.onCreate(SplashActivity.java:93)
            at android.app.Activity.performCreate(Activity.java:5411)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5230)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:780)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:596)
            at dalvik.system.NativeStart.main(Native Method)

任何建议都将不胜感激。感谢。

1 个答案:

答案 0 :(得分:7)

我刚刚创建了一个示例项目,以找出我的问题所在,最后发现我有两个错误。其实其中一个不是因为我,Facebook很疯狂!

1)我忘记将Facebook活动添加到我的清单文件中(这就是为什么我没有得到OnActivityResult的日志)

<!-- Facebook -->
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"/>

2)您 必须 在您的strings.xml文件中创建facebook_app_id并将元数据链接到该文件,而不是直接将您的app_id放在那里。

<meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>

在这些变化之后,我能够在我的项目中看到Facebook对话框。 这些东西浪费了我的两天,希望不会发生在你身上。