com.facebook.share.widget.ShareButton 在 Android 应用中被禁用

com.facebook.share.widget.ShareButton is disabled in Android App

我的 Android 应用程序中有一个 Facebook 分享按钮。当我将它与 ImageButton 或 ImageView onClick 一起使用时,它在调试模式下工作得非常好。但是 APK 构建因发布类型而失败。

我在 Whosebug Question 的帮助下解决了这个问题,但最终导致 FB 分享按钮被禁用。

尝试按照此处几个答案的建议添加以下代码,但仍然无法正常工作。

shareButton.setShareContent(linkContent);

下面是我调用 onClick 函数的 XML。

<com.facebook.share.widget.ShareButton
    android:id="@+id/imgFBShare"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/lblChangePasswordTitle"
    android:layout_alignEnd="@+id/scrollView2"
    android:layout_marginBottom="12dp"
    android:onClick="shareFB"
    app:srcCompat="@drawable/com_facebook_button_icon_blue" />

下面是我的 onClick "shareFB" 代码。

public void shareFB (View view) {
        callbackManager = CallbackManager.Factory.create();

        List<String> permissionNeeds = Arrays.asList("publish_actions");

        //this loginManager helps you eliminate adding a LoginButton to your UI
        loginManager = LoginManager.getInstance();

        loginManager.logInWithPublishPermissions(this, permissionNeeds);

        if (ShareDialog.canShow(ShareLinkContent.class)) {
            Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            Uri imageUri = Uri.parse(main_image_url);

            ShareButton shareButton = (ShareButton) findViewById(R.id.imgFBShare);

            ShareLinkContent linkContent = new ShareLinkContent.Builder()
                    .setContentTitle(short_headline)
                    .setContentDescription(
                            short_description)
                    .setContentUrl(Uri.parse("http://mywebsite.com/android-app-download/"))
                    .setImageUrl(imageUri)
                    .build();

            shareButton.setShareContent(linkContent);

            shareDialog.show(linkContent);
        }
    }

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

我错过了什么吗?

在详细查看了每一个信息之后,我设法通过两个非常简单的步骤找到了解决方案。

  1. 我的 Activity.xml 中的 <com.facebook.share.widget.ShareButton 恢复为 <ImageView。这为定义自己的图像提供了更大的灵活性。
  2. 我从我的 shareFB 中删除了 ShareButton shareButton = (ShareButton) findViewById(R.id.imgFBShare);shareButton.setShareContent(linkContent);

现在一切正常!