title: Android fragment toolbar text not changing
body: <p>I am developing an app with <code>Fragment</code>. When I open the <code>Fragment</code> where not listed in navigation drawer. For example, I have 8 fragments where 7 fragments is listed in navigation drawer and the other one is not listed (<code>Fragment</code> name is sub category <code>Fragment</code>) in navigation drawer. I can change toolbar title of 7 <code>Fragment</code> using below code. But I cannot change the toolbar tittle of <code>Fragment</code> where not listed in navigation drawer.</p>
  
  <p>My <code>HomeActivity.java</code></p>
  
  <pre><code>public class HomeActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
      private Toolbar mToolbar;
      private FragmentDrawer drawerFragment;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_home);
          mToolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(mToolbar);
          getSupportActionBar().setDisplayShowHomeEnabled(true);
          drawerFragment = (FragmentDrawer)
                  getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
          drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
          drawerFragment.setDrawerListener(this);
  
          displayView(0);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public void onDrawerItemSelected(View view, int position) {
          displayView(position);
      }
  
      public void displayView(int position) {
          Fragment fragment = null;
          String title = getString(R.string.app_name);
          switch (position) {
              case 0:
                  fragment = new HomeFragment();
                  title = getString(R.string.title_home);
                  Log.d("LOGTAG", "Home");
                  break;
              case 1:
                  fragment = new CategoriesFragment();
                  title = getString(R.string.title_categories);
                  Log.d("LOGTAG", "Categories");
                  break;
              case 2:
                  fragment = new MyOrdersFragment();
                  title = getString(R.string.title_myorders);
                  Log.d("LOGTAG", "My Orders");
                  break;
              case 3:
                  fragment = new WalletFragment();
                  title = getString(R.string.title_wallet);
                  Log.d("LOGTAG", "Wallet");
                  break;
              case 4:
                  fragment = new AccountSettingsFragment();
                  title = getString(R.string.title_account_settings);
                  Log.d("LOGTAG", "Account Settings");
                  break;
              case 5:
                  fragment = new AboutUsFragment();
                  title = getString(R.string.title_aboutus);
                  Log.d("LOGTAG", "About Us");
                  break;
              case 6:
                  fragment = new ContactUsFragment();
                  title = getString(R.string.title_contactus);
                  Log.d("LOGTAG", "Contact Us");
                  break;
              case 7:
                  fragment = new LogOutFragment();
                  title = getString(R.string.title_logout);
                  Log.d("LOGTAG", "Log out");
                  break;
              case 8:
                  fragment = new SubCategoryFragment();
                  title = getString(R.string.title_sub_categories);
                  Log.d("LOGTAG", "Sub Categories");
                  break;
              default:
                  break;
          }
  
          if (fragment != null) {
              FragmentManager fragmentManager = getSupportFragmentManager();
              FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
              fragmentTransaction.replace(R.id.container_body, fragment);
              fragmentTransaction.commit();
  
              // set the toolbar title
              getSupportActionBar().setTitle(title);
          }
      }
  
      public void loadFragment(int id, Fragment fragment) {
          FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
          ft.replace(id, fragment, fragment.toString());
          ft.addToBackStack(null);
          ft.commit();
      }
  }
  </code></pre>
  
  <p>And the <code>SubCategoryFragment.java</code></p>
  
  <pre><code>public class SubCategoryFragment extends android.support.v4.app.Fragment {
      List&lt;GetDataAdapter&gt; GetDataAdapter1;
      RecyclerView recyclerView;
      RecyclerView.LayoutManager recyclerViewlayoutManager;
      RecyclerView.Adapter recyclerViewadapter;
  
      String GET_JSON_DATA_HTTP_URL;
      String JSON_IMAGE_TITLE_NAME = "image_title";
      String JSON_IMAGE_URL = "image_url";
      String JSON_IMAGE_ID = "id";
  
      JsonArrayRequest jsonArrayRequest ;
      RequestQueue requestQueue ;
  
      public SubCategoryFragment() {
          // Required empty public constructor
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_sub_category, container, false);
  
          HomeActivity activity = new HomeActivity();
          activity.displayView(8); //Occuring error
  
          // Inflate the layout for this fragment
          return rootView;
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
      }
  }
  </code></pre>
  
  <p>When I add <code>activity.displayView(8)</code> in sub category <code>Fragment</code> it shows error.</p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference</p>
  </blockquote>
  
  <p>So, how it possible to change <code>Toolbar</code> text?</p>