2013-10-21 71 views
6

我有我的分享對話的片段中下面的代碼:分享對話框不能正常工作

TabFour.java

public class TabFour extends Fragment { 
private UiLifecycleHelper uiHelper; 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.activity_tab_four, container, false); 

    return rootView; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    uiHelper = new UiLifecycleHelper(getActivity(), callback); 
    uiHelper.onCreate(savedInstanceState); 

    OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class); 
    action.setProperty("book", "https://example.com/book/Snow-Crash.html"); 

    @SuppressWarnings("deprecation") 
    FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(getActivity(), action, "books.reads", "book") 
      .build(); 
    uiHelper.trackPendingDialogCall(shareDialog.present()); 
} 


@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() { 
     @Override 
     public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) { 
      Log.e("Activity", String.format("Error: %s", error.toString())); 
     } 

     @Override 
     public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) { 
      Log.i("Activity", "Success!"); 
     } 
    }); 
} 



@Override 
public void onResume() { 
    super.onResume(); 
    uiHelper.onResume(); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    uiHelper.onSaveInstanceState(outState); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    uiHelper.onPause(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    uiHelper.onDestroy(); 
} 
private Session.StatusCallback callback = new Session.StatusCallback() { 


    @Override 
    public void call(Session session, SessionState state, 
      Exception exception) { 
     // TODO Auto-generated method stub 

    } 
}; 

}

模擬器上的Facebok的共享負載,但是它不在電話上。另外,當共享對話框在模擬器上加載時,由於左上角的共享按鈕被禁用(可見,但無法單擊),因此無法共享它。此外,仿真程序顯示Toast消息「無法提取共享預覽」。請幫忙!

我希望有人能向我解釋這一點。謝謝!

+0

你在你的模擬器上有Facebook應用嗎?並檢查facebook應用的版本。你看到任何adb錯誤日誌嗎? –

回答

0

https://developers.facebook.com/docs/android/share

如果安裝了Facebook應用程序的Facebook共享對話框纔會起作用。因此,建議的方法是首先檢查是否可以顯示共享對話框,如果可以顯示,請將其與您的內容一起顯示或使用Web對話框顯示共享屏幕。即

if (FacebookDialog.canPresentShareDialog(getApplicationContext(), 
              FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) 
{ 
     // Publish the post using the Share Dialog 
     FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this) 
       .setLink("https://developers.facebook.com/android") 
       .build(); 
     uiHelper.trackPendingDialogCall(shareDialog.present()); 

} 
else 
{ 
     // Fallback. For example, publish the post using the Feed Dialog 
} 

這裏是其他選項的例子

private void publishFeedDialog() { 
    Bundle params = new Bundle(); 
    params.putString("name", "Facebook SDK for Android"); 
    params.putString("caption", "Build great social apps and get more installs."); 
    params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps."); 
    params.putString("link", "https://developers.facebook.com/android"); 
    params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png"); 

    WebDialog feedDialog = (
     new WebDialog.FeedDialogBuilder(getActivity(), 
      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(getActivity(), 
          "Posted story, id: "+postId, 
          Toast.LENGTH_SHORT).show(); 
        } else { 
         // User clicked the Cancel button 
         Toast.makeText(getActivity().getApplicationContext(), 
          "Publish cancelled", 
          Toast.LENGTH_SHORT).show(); 
        } 
       } else if (error instanceof FacebookOperationCanceledException) { 
        // User clicked the "x" button 
        Toast.makeText(getActivity().getApplicationContext(), 
         "Publish cancelled", 
         Toast.LENGTH_SHORT).show(); 
       } else { 
        // Generic, ex: network error 
        Toast.makeText(getActivity().getApplicationContext(), 
         "Error posting story", 
         Toast.LENGTH_SHORT).show(); 
       } 
      } 

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

不要總是依靠模擬器檢查的事情了。畢竟它只是一個「模擬器」。您可以輕鬆在真實設備上預覽您的應用。所以如果事情沒有按照它應該在模擬器上運行,在真實設備上驗證它。 (當然,我同意,當你沒有多個設備,屏幕尺寸等時,仿真器是好的)

PS:你還需要在fb開發者網站的應用程序設置頁面上正確設置Key Hashes。