title: How to set onclicklistener from a different activitys class?
body: <p>How can I set <code>onClickListener</code> of a button that belongs to a different activity?
  I am using <code>sharedPreferences</code> and I want to create a logout button on a different activity. The activity which opens at launch should not have the logout button. That's why I don't have all the <code>sharedPreferences</code> code in the profile activity.</p>
  
  <p><strong>HomeActivity class</strong>:</p>
  
  <pre><code>//UserSessionManager class
  UserSessionManager session;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_home);
  
      //start new activity when fab is clicked
      final FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent intent = new Intent(getApplicationContext(), Profile.class);
              startActivity(intent);
          }
      });
  
      session = new UserSessionManager(getApplicationContext());
  
      //check if user is logged in
      //if the user is not logged in
      if (session.checkLogin()) {
          finish();
      }
  
      HashMap&lt;String, String&gt; user = session.getUserDetails();
  
      //get variables
      String username = user.get(UserSessionManager.KEY_USERNAME);
      String age = user.get(UserSessionManager.KEY_AGE);
      String firstName = user.get(UserSessionManager.KEY_FIRSTNAME);
  
  
      EditText etUsername = (EditText) findViewById(R.id.etEmail);
      EditText etAge = (EditText) findViewById(R.id.etAge);
  </code></pre>
  
  <p>I want the logout button to be in the profile activity.</p>
  
  <p>Here is the <strong>profile class</strong>:</p>
  
  <pre><code>Button bLogOut;
  UserSessionManager sessionManager;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_profile);
  }
  public void sendSession(final UserSessionManager userSession) {
      bLogOut = (Button)findViewById(R.id.bLogOut);
  
      bLogOut.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              userSession.logoutUser();
          }
      });
  }
  </code></pre>
  
  <p>After doing many different things I created the send session method which receives the session variable from the <code>HomeActivity</code> class as a parameter. I still got a <code>nullpointerexception</code>.</p>