title: android facebook login and publish button
body: <p>I am trying to make an application which can have only one button to login and post facebook feed using feed dialog, I have read the tutorial from facebook for login in and feed dialog which is <a href="https://developers.facebook.com/docs/howtos/androidsdk/3.0/login-with-facebook/" rel="nofollow">https://developers.facebook.com/docs/howtos/androidsdk/3.0/login-with-facebook/</a> and <a href="https://developers.facebook.com/docs/howtos/androidsdk/3.0/feed-dialog/" rel="nofollow">https://developers.facebook.com/docs/howtos/androidsdk/3.0/feed-dialog/</a>. But in these two tutorials, they are using two button to do the login and publish feed action. Here is the code that what I am doing right now and it gives me error.</p>
  
  <p>This is my main class:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>This my Fragment class:</p>
  
  <pre><code>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 &amp;&amp;
             (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();
  }
  }
  </code></pre>
  
  <p>my errors from logcat:</p>
  
  <pre><code>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.&lt;init&gt;(WebDialog.java:392)
  03-07 21:05:52.527: E/AndroidRuntime(2052):     at com.facebook.widget.WebDialog$FeedDialogBuilder.&lt;init&gt;(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)
  </code></pre>
  
  <p>Can someone tell me why is this error happen and what can I do to fix the error and make the button work? </p>
  
  <p>I am using the latest facebook sdk, it is really confusing for me, is there any other tutoriala that I can try to letorn the facebook sdk?</p>
  
  <p>This is my first time I use this site, if I am doing anything wrong please let me know and I will try to do it right.</p>
  
  <p>Thanks.</p>