Facebook SDK:在创建WebDialog.FeedDialogBu​​ilder时没有“打开”会话状态

时间:2014-09-22 16:55:51

标签: android facebook session fbwebdialogs

我在实施Facebook共享信息时遇到了这样一个奇怪的错误。 我在Facebook教程Facebook tutorial - share info

中描述了所有阶段

但是当我尝试创建 WebDialog.feedDialog

的示例时

    private void publishFeedDialog() {
        Bundle params = new Bundle();
        params.putString("name", getString(R.string.name_fb));
        params.putString("description", getString(R.string.description_fb));
        params.putString("link", getString(R.string.share_link_fb));
        params.putString("picture", getString(R.string.pictute_url_fb));
        
        Session session = Session.getActiveSession();
        
        Log.i(TAG, "session = " + session + " isOpen = " + session.isOpened() + " isClosed = " + session.isClosed());
        
        WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(this, Session.getActiveSession(), params))
        		.setOnCompleteListener(new OnCompleteListener() {

                @Override
                public void onComplete(Bundle values, FacebookException error) {
                    if (error == null) {
                    	
                        // When the story is posted, echo the success and the post Id.
                        final String postId = values.getString("post_id");
                        
                        if (postId != null) {
                            Toast.makeText(MainActivity.this, "Posted story, id: " + postId,
                                Toast.LENGTH_SHORT).show();
                        } else {
                            // User clicked the Cancel button
                            Toast.makeText(MainActivity.this, "Publish cancelled", 
                                Toast.LENGTH_SHORT).show();
                        }
                        
                    } else if (error instanceof FacebookOperationCanceledException) {
                        // User clicked the "x" button
                        Toast.makeText(MainActivity.this, "Publish cancelled", Toast.LENGTH_SHORT).show();
                        
                    } else {
                        // Generic, ex: network error
                        Toast.makeText(MainActivity.this, "Error posting story", Toast.LENGTH_SHORT).show();
                    }
                }

            }).build();
        
        feedDialog.show();
    }

我抓住了 FacebookException

09-22 19:34:49.325: E/ActivityThread(12202): Failed to find provider info for com.facebook.katana.provider.PlatformProvider
09-22 19:34:49.325: I/MainActivity(12202): session = {Session state:CREATED, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}, appId:1111111111111} isOpen = false isClosed = false
09-22 19:34:49.325: D/AndroidRuntime(12202): Shutting down VM
09-22 19:34:49.325: W/dalvikvm(12202): threadid=1: thread exiting with uncaught exception (group=0x418cc700)
09-22 19:34:49.325: E/ActivityThread(12202): Failed to find provider info for com.facebook.wakizashi.provider.PlatformProvider
09-22 19:34:49.405: D/dalvikvm(12202): GC_FOR_ALLOC freed 891K, 9% free 10493K/11452K, paused 21ms, total 21ms
09-22 19:34:52.365: E/AndroidRuntime(12202): FATAL EXCEPTION: main
09-22 19:34:52.365: E/AndroidRuntime(12202): java.lang.IllegalStateException: Could not execute method of the activity
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at android.view.View$1.onClick(View.java:3633)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at android.view.View.performClick(View.java:4240)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at android.view.View$PerformClick.run(View.java:17721)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at android.os.Handler.handleCallback(Handler.java:730)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at android.os.Looper.loop(Looper.java:137)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at android.app.ActivityThread.main(ActivityThread.java:5103)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at java.lang.reflect.Method.invokeNative(Native Method)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at java.lang.reflect.Method.invoke(Method.java:525)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at dalvik.system.NativeStart.main(Native Method)
09-22 19:34:52.365: E/AndroidRuntime(12202): Caused by: java.lang.reflect.InvocationTargetException
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at java.lang.reflect.Method.invokeNative(Native Method)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at java.lang.reflect.Method.invoke(Method.java:525)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at android.view.View$1.onClick(View.java:3628)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	... 11 more
09-22 19:34:52.365: E/AndroidRuntime(12202): Caused by: com.facebook.FacebookException: Attempted to use a Session that was not open.
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at com.facebook.widget.WebDialog$BuilderBase.<init>(WebDialog.java:485)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at com.facebook.widget.WebDialog$FeedDialogBuilder.<init>(WebDialog.java:669)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at com.kaeriasarl.psslite.activities.MainActivity.publishFeedDialog(MainActivity.java:131)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at com.kaeriasarl.psslite.activities.MainActivity.shareLinkToFacebook(MainActivity.java:116)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	at com.kaeriasarl.psslite.activities.MainActivity.onClickFeature(MainActivity.java:99)
09-22 19:34:52.365: E/AndroidRuntime(12202): 	... 14 more
09-22 19:34:52.475: I/GAV3(12202): Thread[GAThread,5,main]: No campaign data found.

正如我从日志中可以理解的那样,主要的麻烦是 Session.getActiveSession()的结果不应该为空并且应该打开但是因为我看到会话具有状态只创建了! 但我做了Facebook教程所说的所有阶段!求救!

2 个答案:

答案 0 :(得分:7)

经过调查我解决了我的问题:

1)在创建feedDialog之前,我应该检查Active Session是否打开,如果没有,那么我应该直接打开Active Session

	if (Session.getActiveSession() == null || !Session.getActiveSession().isOpened()) {
        Session.openActiveSession(MainActivity.this, true, callback);
    } else {
        publishFeedDialog();
    }

2)并在Session.StatusCallback中调用publishFeedDialog()

    private Session.StatusCallback callback = new Session.StatusCallback() {
		
		@Override
		public void call(Session session, SessionState state, Exception exception) {
			
			if (state.isOpened() && isFbShare) {
				publishFeedDialog();
			}
		}
	};

答案 1 :(得分:0)

除了上面接受的答案之外,如果这是第一次用户体验流程,用户将需要登录才能创建会话。为此,您需要将LoginActivity添加到AndroidManifest.xml -

    <activity
        android:name="com.facebook.LoginActivity"
        android:label="@string/title_activity_facebook_login" >
    </activity>