使用Facebook app邀请为Android时出错

时间:2013-07-30 08:33:06

标签: android facebook facebook-android-sdk

我有一个Android应用程序,我需要邀请Facebook的朋友。我使用下面的代码

WebDialog requestsDialog = (
                new WebDialog.RequestsDialogBuilder(FacebookLogin.this,
                    Session.getActiveSession(),
                    params))
                    .setOnCompleteListener(new OnCompleteListener() {

                        @Override
                        public void onComplete(Bundle values,
                            FacebookException error) {
                            if (error != null) {
                                if (error instanceof FacebookOperationCanceledException) {
                                    Toast.makeText(FacebookLogin.this.getApplicationContext(), 
                                        "Request cancelled", 
                                        Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(FacebookLogin.this.getApplicationContext(), 
                                        "Network Error", 
                                        Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                final String requestId = values.getString("request");
                                if (requestId != null) {
                                    Toast.makeText(FacebookLogin.this.getApplicationContext(), 
                                        "Request sent",  
                                        Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(FacebookLogin.this.getApplicationContext(), 
                                        "Request cancelled", 
                                        Toast.LENGTH_SHORT).show();
                                }
                            }   
                        }

                    })
                    .build();
            requestsDialog.show();

但它在“if(错误实例FacebookOperationCanceledException)”行引发语法错误“不兼容的条件操作数类型FacebookException和FacebookOperationCanceledException”。 我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我认为您需要将 apprequests 添加到WebDialog.RequestsDialogBuilder

这就是我使用它的方式:

dialog = new WebDialog.Builder(activity, Session.getActiveSession(), "apprequests", params).
    setOnCompleteListener(new WebDialog.OnCompleteListener()
    {
        @Override
        public void onComplete(Bundle values, FacebookException error)
        {
            if (error != null && !(error instanceof FacebookOperationCanceledException))
            {
                // error
            }
            else
            {
                // good
            }
            dialog = null;
        }
    }).build();

Window dialogWindow = dialog.getWindow();
dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

dialog.show();

或者,此外,您可以使用此SimpleFacebook库,通过使用比Facebook SDK 3.0更简单的API,使生活更轻松。这个库包装了Facebook SDK 3.0:

以下是使用此库邀请的方式:

OnInviteListener onInviteListener = new SimpleFacebook.OnInviteListener()
{

    @Override
    public void onFail()
    {
        // insure that you are logged in before inviting
        Log.w(TAG, "Failed to invite");
    }

    @Override
    public void onException(Throwable throwable)
    {
        Log.e(TAG, "Bad thing happened", throwable);
    }

    @Override
    public void onComplete()
    {
        Log.i(TAG, "Invitation was sent");
    }
};

mSimpleFacebook.invite(MainActivity.this, "I invite you to use this app", onInviteListener);

希望,我可以帮助你。