在Android中向Facebook好友发送应用请求

时间:2013-09-18 09:20:59

标签: android facebook facebook-graph-api

我正在尝试向fb朋友发送应用请求但未能做到。我已经尝试过Hackbook示例代码。但我没有收到任何请求。

以下是代码:

 Bundle params = new Bundle();
          params.putString("message", "Learn how to make your Android apps social");         
          Utility.mFacebook.dialog(Hackbook.this, "apprequests", params,new AppRequestsListener());

public class AppRequestsListener extends BaseDialogListener {
        @Override
        public void onComplete(Bundle values) {
            Toast toast = Toast.makeText(getApplicationContext(), "App request sent",Toast.LENGTH_SHORT);
            toast.show();
        }

        @Override
        public void onFacebookError(FacebookError error) {
            Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel() {
            Toast toast = Toast.makeText(getApplicationContext(), "App request cancelled",
                    Toast.LENGTH_SHORT);
            toast.show();
        }
    }

正在显示使用上面的代码Toast消息,但我没有收到任何请求。

我也尝试过其他代码..

这是代码:

private void sendRequestDialog() {
        Bundle params = new Bundle();
        params.putString("message",
                "Learn how to make your Android apps social");

        WebDialog requestsDialog = (new WebDialog.RequestsDialogBuilder(
                Hackbook.this, Session.getActiveSession(), params))
                .setOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(Bundle values,
                            FacebookException error) {
                        if (error != null) {
                            if (error instanceof FacebookOperationCanceledException) {
                                Toast.makeText(
                                        Hackbook.this.getApplicationContext(),
                                        "Request cancelled", Toast.LENGTH_SHORT)
                                        .show();
                            } else {
                                Toast.makeText(
                                        Hackbook.this.getApplicationContext(),
                                        "Network Error", Toast.LENGTH_SHORT)
                                        .show();
                            }
                        } else {
                            final String requestId = values
                                    .getString("request");
                            if (requestId != null) {
                                Toast.makeText(
                                        Hackbook.this.getApplicationContext(),
                                        "Request sent", Toast.LENGTH_SHORT)
                                        .show();
                            } else {
                                Toast.makeText(
                                        Hackbook.this.getApplicationContext(),
                                        "Request cancelled", Toast.LENGTH_SHORT)
                                        .show();
                            }
                        }
                    }
                }).build();
        requestsDialog.show();
    }

使用上面的代码我收到错误:

09-18 14:42:50.102: E/InputEventReceiver(1310): Exception dispatching input event.
09-18 14:42:50.102: E/MessageQueue-JNI(1310): Exception in MessageQueue callback: handleReceiveCallback
09-18 14:42:50.142: E/MessageQueue-JNI(1310): java.lang.NullPointerException: Argument 'session' cannot be null
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at com.facebook.internal.Validate.notNull(Validate.java:29)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at com.facebook.widget.WebDialog$BuilderBase.<init>(WebDialog.java:392)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at com.facebook.widget.WebDialog$RequestsDialogBuilder.<init>(WebDialog.java:684)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at com.facebook.android.Hackbook.sendRequestDialog(Hackbook.java:520)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at com.facebook.android.Hackbook.onItemClick(Hackbook.java:230)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.widget.AbsListView.performItemClick(AbsListView.java:1268)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.widget.AbsListView$PerformClick.run(AbsListView.java:3055)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.widget.AbsListView.onTouchEvent(AbsListView.java:3961)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.View.dispatchTouchEvent(View.java:7241)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2179)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1914)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2113)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1466)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.app.Activity.dispatchTouchEvent(Activity.java:2436)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2061)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.View.dispatchPointerEvent(View.java:7425)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.os.MessageQueue.nativePollOnce(Native Method)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.os.MessageQueue.next(MessageQueue.java:125)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.os.Looper.loop(Looper.java:124)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at android.app.ActivityThread.main(ActivityThread.java:4918)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at java.lang.reflect.Method.invokeNative(Native Method)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at java.lang.reflect.Method.invoke(Method.java:511)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-18 14:42:50.142: E/MessageQueue-JNI(1310):   at dalvik.system.NativeStart.main(Native Method)
09-18 14:42:50.152: D/AndroidRuntime(1310): Shutting down VM
09-18 14:42:50.152: W/dalvikvm(1310): threadid=1: thread exiting with uncaught exception (group=0x40ef7438)

有人可以帮我解决。@谢谢

3 个答案:

答案 0 :(得分:0)

WebDialog requestsDialog = (new WebDialog.RequestsDialogBuilder(Hackbook.this, Session.getActiveSession(), params))

错误是会话变量为空。请确保您在先前的代码中正确初始化它。

答案 1 :(得分:0)

我没有看到任何初始化Session变量的代码。

您获得代码的官方Facebook页面上显示以下内容

“先决条件

在开始之前,请确保您已经设置了Facebook登录。这可确保您具备先决条件,并且您的应用已准备好进行其他Facebook集成。 “

我的猜测是用户没有活动会话。如果它没有要求您登录到Facebook或者您尚未登录,那么您将没有有效的会话,这意味着“Session.getActiveSession()”将返回null。

答案 2 :(得分:0)

首先看到这个链接:

Facebook Session

另见Android SDK for Facebook App Tutorial

你试过这个:

    <application android:label="@string/app_name"
                 android:icon="@drawable/icon"
                 android:theme="@android:style/Theme.NoTitleBar"
            >
        <activity android:name=".HelloFacebookSampleActivity"
                  android:label="@string/app_name"
                  android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.LoginActivity"
                  android:theme="@android:style/Theme.Translucent.NoTitleBar"
                  android:label="@string/app_name" />
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
    </application>

@string/app_id

中定义了meta标记的string.xml属性

编辑:

尝试搜索sessionId存储位置和使用位置。

也许您在存储sessionId时遇到问题并在获取当前sessionId

时收到错误

快乐编码:)