title: Add back arrow in fragment
body: <p>I have a framelayout in MainActivity and I've attached TabFragment fragment to the activity. The TabFragment has TabLayout as well as Navigation Drawer. I used this link's recommended answer to style toolbar in TabFragment. But when I use <code>((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);</code> in a NewFragment(it opens on button click from one of the tab) the app crashes with NullPointerException. I want the back arrow without using any drawable icon. Thanks for any help in this regard.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>FATAL EXCEPTION: main
                                                                      Process: com.sam.pagersliding, PID: 7652
                                                                      java.lang.NullPointerException
                                                                          at com.sam.pagersliding.NewFragment.onCreate(NewFragment.java:27)
                                                                          at android.support.v4.app.Fragment.performCreate(Fragment.java:1951)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1029)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                          at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                                                                          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                          at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                          at android.os.Handler.handleCallback(Handler.java:808)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                          at android.os.Looper.loop(Looper.java:193)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5388)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
                                                                          at dalvik.system.NativeStart.main(Native Method)</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>public class NewFragment extends Fragment{
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          setHasOptionsMenu(true);
      }
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.newfrag, container, false);
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
      }
  
  
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          switch (id){
              case android.R.id.home:
                  getActivity().onBackPressed();
                  break;
          }
          return super.onOptionsItemSelected(item);
      }
  
  
  
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>public class TabFragment extends Fragment{
  
      Toolbar toolbar;
      private TabLayout tabLayout;
      private ViewPager viewPager;
  
      public static DrawerLayout mDrawerLayout;
      NavigationView navigationView;
      ActionBarDrawerToggle mDrawerToggle;
  
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.tabfrag, container, false);
  
  
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          toolbar = (Toolbar) view.findViewById(R.id.toolbar);
          tabLayout = (TabLayout) view.findViewById(R.id.tabs);
          viewPager = (ViewPager) view.findViewById(R.id.viewpager);
          mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
          navigationView = (NavigationView) view.findViewById(R.id.navigation_view);
  
          setupViewPager(viewPager);
          tabLayout.setupWithViewPager(viewPager);
  
          mDrawerToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout, toolbar,R.string.app_name,R.string.app_name);
          mDrawerToggle.syncState();
  
          navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
              @Override
              public boolean onNavigationItemSelected(MenuItem menuItem) {
                  mDrawerLayout.closeDrawers();
  
  
  
                  if (menuItem.getItemId() == R.id.nav_one) {
                      NavOne nav1 = new NavOne();
                      ((MainActivity)getActivity()).replaceFragment(nav1);
                      mDrawerLayout.closeDrawer(navigationView);
  
                  }
  
                  if (menuItem.getItemId() == R.id.nav_two) {
                      NavTwo nav2 = new NavTwo();
                      ((MainActivity)getActivity()).replaceFragment(nav2);
                      mDrawerLayout.closeDrawer(navigationView);
  
                  }
  
                  return false;
              }
  
          });
      }
  
      private void setupViewPager(ViewPager viewPager) {
          ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
          adapter.addFragment(new OneFragment(), "ONE");
          adapter.addFragment(new TwoFragment(), "TWO");
          viewPager.setAdapter(adapter);
      }
  
      class ViewPagerAdapter extends FragmentPagerAdapter {
          private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
          private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
          public ViewPagerAdapter(FragmentManager manager) {
              super(manager);
          }
  
          @Override
          public Fragment getItem(int position) {
              return mFragmentList.get(position);
          }
  
          @Override
          public int getCount() {
              return mFragmentList.size();
          }
  
          public void addFragment(Fragment fragment, String title) {
              mFragmentList.add(fragment);
              mFragmentTitleList.add(title);
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              return mFragmentTitleList.get(position);
          }
      }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public class MainActivity extends AppCompatActivity {
  
      FragmentManager myFragmentManager;
      FragmentTransaction myFragmentTransaction;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Attach main fragment to activity
          if (findViewById(R.id.containerView) != null) {
              if (savedInstanceState != null) {
                  return;
              }
  
              myFragmentManager = getSupportFragmentManager();
              myFragmentTransaction = myFragmentManager.beginTransaction();
              myFragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
          }
  
  
  
      }
  
      public void replaceFragment(Fragment fragment){
          FragmentTransaction t = getSupportFragmentManager().beginTransaction();
          t.replace(R.id.containerView, fragment).addToBackStack(null);
          t.commit();
      }
  
  
  
  }</code></pre>
  </div>
  </div>
  </p>