Facebook喜欢按钮在Android应用程序中不起作用

时间:2015-05-28 14:56:01

标签: android facebook facebook-like

我已经按照一些教程如何在Android应用中使用facebook。我设法实现了工作“登录”按钮和“共享”按钮,但我不能使按钮正常工作。在我按下它后,我得到这个问题图像出现一秒钟,然后它消失了:

enter image description here

这是我的应用程序代码(其中包含片段的简单活动):

public class FacebookFragment extends Fragment {
    private View rootView;
    private TextView simpleTextView;
    private CallbackManager callbackManager;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //PODSTAWOWE RZECZY
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext()); //facebook
        callbackManager = CallbackManager.Factory.create();
        registerCallback();

        //JAKIEŚ TOKENY I INNE TRACKERY DO UPDATÓW
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                Toast.makeText(getActivity().getApplicationContext(), "currentAccessTokenChanged", Toast.LENGTH_LONG).show();
                simpleTextView.setText("current access token changed");
            }
        };
        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                // App code
                if (currentProfile != null) {
                    ((TextView) rootView.findViewById(R.id.textView)).setText("Welcome " + currentProfile.getName());
                } else {
                    Toast.makeText(getActivity().getApplicationContext(), "Profil null", Toast.LENGTH_LONG).show();
                }
            }
        };
        //ZACZYNAMY ŚLEDZIĆ
        profileTracker.startTracking();
        accessTokenTracker.startTracking();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_facebook, container, false);

        //SPRAWDZAMY CZY ZALOGOWANY JEST UZYTKOWNIK I ROBIMY SET-UP PROGRAMU
        simpleTextView = (TextView) rootView.findViewById(R.id.textView);
        checkIfLogged();
        setFacebookLoginButton();
        setFacebookLikeButton();
        setFacebookShareButton();

        // Inflate the layout for this fragment
        return rootView;
    }

    @Override
    public void onStop() {
        super.onStop();
        //STOPUJEMY TRACKING
        accessTokenTracker.stopTracking();
        profileTracker.stopTracking();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

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


    //========================= CUSTOM METHODS ========================================
    private void checkIfLogged(){
        Profile profile = Profile.getCurrentProfile();
        if (AccessToken.getCurrentAccessToken() != null) {
            simpleTextView.setText("Welcome " + profile.getName());
        } else {
            Toast.makeText(getActivity().getApplicationContext(), "not logged in", Toast.LENGTH_LONG).show();
        }
    }

    private void setFacebookLoginButton(){
        LoginButton facebookButton = (LoginButton) rootView.findViewById(R.id.login_button);
        facebookButton.setFragment(this);
    }
    private void registerCallback(){
        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                    }
                    @Override
                    public void onCancel() {
                        Toast.makeText(getActivity().getApplicationContext(), "too bad you didn't log in...", Toast.LENGTH_LONG).show();
                    }
                    @Override
                    public void onError(FacebookException exception) {
                        Toast.makeText(getActivity().getApplicationContext(), "that's that error shit...", Toast.LENGTH_LONG).show();
                    }
                });
    }
    private void setFacebookLikeButton(){
        LikeView likeView = (LikeView) rootView.findViewById(R.id.like_button);
        likeView.setLikeViewStyle(LikeView.Style.STANDARD);
        likeView.setFragment(this);
        likeView.setOnErrorListener(new LikeView.OnErrorListener() {
            @Override
            public void onError(FacebookException e) {
                Toast.makeText(getActivity().getApplicationContext(), "masz chuja nie lajka...", Toast.LENGTH_LONG).show();
            }
        });
        likeView.setObjectIdAndType("https://www.facebook.com/FacebookDevelopers", LikeView.ObjectType.PAGE);
    }
    private void setFacebookShareButton(){
        ShareLinkContent content = new ShareLinkContent.Builder()
                .setContentUrl(Uri.parse("http://joemonster.org"))
                .setImageUrl(Uri.parse("http://joemonster.org/i/2015/05/pasazer.jpg")).
                setContentDescription("Prawdopodobnie najlepsza strona we wszechświecie!")
                .setContentTitle("Joe Monster")
                .build();

        ShareButton shareButton = (ShareButton)rootView.findViewById(R.id.share_button);
        shareButton.setFragment(this);
        shareButton.setShareContent(content);
    }
}

其他有关行为:

  • 如果我使用“分享”按钮登录,那么我的“登录”按钮不会更改为显示“退出”。如果我然后单击“登录”按钮并取消它。我将不得不再次使用“分享”或“赞”按钮“登录”。
  • 如果我使用“登录”按钮登录,那么下面就像按钮一样,我看到这个网站有多少喜欢。

我的虚拟设备中没有安装Facebook应用程序。

1 个答案:

答案 0 :(得分:1)

使用Like按钮需要应用程序批准,因此在开发期间,只有应用程序中具有Admin / Developer / Tester角色的用户才能访问它。一旦获得批准,任何用户都可以使用该功能。