1

在实现 Facebook 共享信息时,我遇到了这样一个奇怪的错误。我制作了 Facebook 教程中描述的所有阶段Facebook 教程 - 分享信息

但是当我试图创建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 教程所说的那样完成了所有阶段!请帮忙!

4

2 回答 2

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

于 2014-09-23T13:57:53.717 回答
0

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

    <activity
        android:name="com.facebook.LoginActivity"
        android:label="@string/title_activity_facebook_login" >
    </activity>
于 2015-02-12T04:13:48.813 回答