0

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

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 (error instanceof FacebookOperationCanceledException)”行抛出语法错误“不兼容的条件操作数类型 FacebookException 和 FacebookOperationCanceledException”。我错过了什么吗?

4

1 回答 1

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);

希望,我可以帮助你。

于 2013-07-31T17:43:35.340 回答