我正在尝试制作一个应用程序,它只能有一个按钮来登录并使用提要对话框发布 Facebook 提要,我已经阅读了来自 facebook 的登录和提要对话框的教程,即https://developers.facebook.com/docs/ howtos/androidsdk/3.0/login-with-facebook/和https://developers.facebook.com/docs/howtos/androidsdk/3.0/feed-dialog/。但是在这两个教程中,他们使用两个按钮来执行登录和发布提要操作。这是我现在正在做的代码,它给了我错误。
这是我的主要课程:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
public class Main extends FragmentActivity {
private MainFragment mainFragment;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment(this, Main.this);
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, mainFragment)
.commit();
} else {
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
}
这是我的片段类:
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.facebook.FacebookException;
import com.facebook.FacebookOperationCanceledException;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.WebDialog;
import com.facebook.widget.WebDialog.OnCompleteListener;
public class MainFragment extends Fragment{
private UiLifecycleHelper uiHelper;
private Button shuffle, fbshare;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
fbshare = (Button) view.findViewById(R.id.share);
fbshare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
publishFeedDialog();
}
});
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
fbshare.setOnClickListener(new View.OnClickListener() {
} else if (state.isClosed()) {
}
}
private void publishFeedDialog() {
Session session = Session.getActiveSession();
if (session == null){
session = new Session(getActivity());
Session.setActiveSession(session);
}
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();
}
}
我的 logcat 错误:
03-07 21:05:52.527: E/AndroidRuntime(2052): FATAL EXCEPTION: main
03-07 21:05:52.527: E/AndroidRuntime(2052): com.facebook.FacebookException: Attempted to use a Session that was not open.
03-07 21:05:52.527: E/AndroidRuntime(2052): at com.facebook.widget.WebDialog$BuilderBase.<init>(WebDialog.java:392)
03-07 21:05:52.527: E/AndroidRuntime(2052): at com.facebook.widget.WebDialog$FeedDialogBuilder.<init>(WebDialog.java:553)
03-07 21:05:52.527: E/AndroidRuntime(2052): at com.example.projectapp.MainFragment.publishFeedDialog(MainFragment.java:306)
03-07 21:05:52.527: E/AndroidRuntime(2052): at com.example.projectapp.MainFragment.access$11(MainFragment.java:293)
3-07 21:05:52.527: E/AndroidRuntime(2052): at com.example.projectapp.MainFragment$3.onClick(MainFragment.java:221)
03-07 21:05:52.527: E/AndroidRuntime(2052): at android.view.View.performClick(View.java:4084)
03-07 21:05:52.527: E/AndroidRuntime(2052): at android.view.View$PerformClick.run(View.java:16966)
03-07 21:05:52.527: E/AndroidRuntime(2052): at android.os.Handler.handleCallback(Handler.java:615)
03-07 21:05:52.527: E/AndroidRuntime(2052): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 21:05:52.527: E/AndroidRuntime(2052): at android.os.Looper.loop(Looper.java:137)
03-07 21:05:52.527: E/AndroidRuntime(2052): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-07 21:05:52.527: E/AndroidRuntime(2052): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 21:05:52.527: E/AndroidRuntime(2052): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 21:05:52.527: E/AndroidRuntime(2052): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-07 21:05:52.527: E/AndroidRuntime(2052): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-07 21:05:52.527: E/AndroidRuntime(2052): at dalvik.system.NativeStart.main(Native Method)
有人可以告诉我为什么会发生这个错误,我能做些什么来修复错误并使按钮工作?
我正在使用最新的 facebook sdk,这对我来说真的很困惑,有没有其他教程可以让我尝试让 facebook sdk 失效?
这是我第一次使用这个网站,如果我做错了什么,请告诉我,我会努力做对的。
谢谢。