Passing Session Info Between Activities (Facebook Post on Wall Sample)
<p>I'm trying to write a simple 2 screen application where in 1st screen user gets   authenticated to Facebook and 2nd screen post on user's wall clicking simple button.</p>

<p>I'm receiving a NullPointerException when I click on PostonWall button and I believe it's related with passing session information between activities. But I cannot figure out how to restore session info.</p>

<p>What am I doing wrong here? Any suggestion highly appreciated!</p>

<p>Thanks in advance.</p>

<p>Here is my code for 1st screen.</p>

<pre><code>public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    private static final String FACEBOOK_APPID = "XXXXXX";
    private static final String FACEBOOK_PERMISSION = "publish_stream";
    public FacebookConnector facebookConnector;
    private TextView FacebookStatus;
    private Button FacebookButton, ButtonNextScreen;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        FacebookStatus=(TextView)findViewById(R.id.textView1);
        FacebookButton=(Button)findViewById(R.id.button1);
        ButtonNextScreen=(Button)findViewById(R.id.ButtonNextScreen);

        this.facebookConnector = new FacebookConnector(FACEBOOK_APPID, this, getApplicationContext(), new String[] {FACEBOOK_PERMISSION});

        FacebookButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {

                    @Override
                    public void onAuthSucceed() {
                        SessionStore.save(facebookConnector.getFacebook(), getApplicationContext());
                        FacebookStatus.setText("Facebook Authenticated!!");
                    }

                    @Override
                    public void onAuthFail(String error) {

                    }
                };
                SessionEvents.addAuthListener(listener);
                facebookConnector.login();              
            }
        });

        ButtonNextScreen.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Intend to go second screen (SendScreen)
                Intent i=new Intent(v.getContext(),SecondClass.class);
                startActivity(i);               
            }
        });


    }
}
</code></pre>

<p>////////////////FacebookConnector Method,</p>

<pre><code>public FacebookConnector(String appId,Activity activity,Context context,String[] permissions) {
        this.facebook = new Facebook(appId);

        SessionStore.restore(facebook, context);
        SessionEvents.addAuthListener(mSessionListener);
        SessionEvents.addLogoutListener(mSessionListener);

        this.context=context;
        this.permissions=permissions;
        this.mHandler = new Handler();
        this.activity=activity;
    }
</code></pre>

<p>///////////////Session Save and Session Methods</p>

<pre><code>public class SessionStore {

    private static final String TOKEN = "access_token";
    private static final String EXPIRES = "expires_in";
    private static final String KEY = "facebook-session";

    public static boolean save(Facebook session, Context context) {
        Editor editor =
            context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(TOKEN, session.getAccessToken());
        editor.putLong(EXPIRES, session.getAccessExpires());
        return editor.commit();
    }

    public static boolean restore(Facebook session, Context context) {
        SharedPreferences savedSession =
            context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
        session.setAccessToken(savedSession.getString(TOKEN, null));
        session.setAccessExpires(savedSession.getLong(EXPIRES, 0));
        return session.isSessionValid();
    }
</code></pre>

<p>////////// Finally the Second Class</p>

<p>public class SecondClass extends Activity{</p>

<pre><code>private static final String TAG = "FacebookSample";
private static final String MSG = "Message from FacebookSample";
private Button FacebookPostButton;
private FacebookConnector facebookConnector; // I THINK THIS SHOULD BE THE ISSUE
private final Handler mFacebookHandler = new Handler();

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        FacebookPostButton=(Button)findViewById(R.id.FacebookPostButton);               
        FacebookPostButton.setOnClickListener(new OnClickListener() {               
        public void onClick(View v) {                   
        if (SessionStore.restore(facebookConnector.getFacebook(), getApplicationContext()))                         
                {
                    postMessageInThread();
                }                   
            }
        });
</code></pre>

<p>}</p>
