title: Views in Fragment Return NullPointerException After Selecting Navigation Item
body: <p>I have a navigation drawer in an activity that will update a fragment in the activity. All works fine, but when I rotate the device and press one of the navigation drawer items, it crashes because views in the fragment are null.</p>
  
  <p>Here where I implement navigation items selection. I pass data to an interface method that the fragment implements.</p>
  
  <pre><code>    @Override
  public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  
      // Call interface method that GalleriesFragment implements
      UpdateGalleries updateGalleries =  (UpdateGalleries) getSupportFragmentManager().getFragments().get(0);
      updateGalleries.fetchGalleriesOf(mSelectedCategory, categoryId);
  
      DrawerLayout drawer = findViewById(R.id.drawer_layout);
      drawer.closeDrawer(GravityCompat.START);
  
      return false;
  }
  </code></pre>
  
  <p>Here is the portion of the fragment where I implement the interface method:</p>
  
  <pre><code>    @Override
  public void fetchGalleriesOf(String category, String categoryId) {
  
      mProgressBar.setVisibility(View.VISIBLE); // progressBar is null
  
      // Listen for firebase values
      listenToGalleries();
  
      // Attach listener to reference
      if(category.equals("Home")){
          category = "Recently added";
          References.galleriesRef.orderByChild(Constants.TIMESTAMP).addValueEventListener(mGalleriesListener);
      }else {
          References.galleriesRef.orderByChild(Constants.CATEGORY_ID).equalTo(categoryId)
                  .addValueEventListener(mGalleriesListener);
      }
  
      mCategoryTV.setText(category);
  
  }// End fetchGalleriesOf
  </code></pre>
  
  <p>Here is the fragment in activity_main.xml:</p>
  
  <pre><code>&lt;fragment xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:name="com.thunderbolt.thebolt.ui.fragment.GalleriesFragment"
  tools:context=".ui.activity.MainActivity"/&gt;
  </code></pre>
  
  <p>The mProgressBar and mCategoryTV in the fragment return null pointer exception despite initializing them in onCreateView of the fragment:</p>
  
  <pre><code>    @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View rootView = inflater.inflate(R.layout.fragment_gallery, container, false);
  
      // Reset list
      mGalleries = new ArrayList&lt;&gt;();
      mGalleriesAdapter = new GalleriesAdapter(mGalleries, this);
  
      mRecyclerView = rootView.findViewById(R.id.rv_galleries);
      mRecyclerView.setAdapter(mGalleriesAdapter);
  
      LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
      mRecyclerView.setLayoutManager(layoutManager);
      mRecyclerView.setHasFixedSize(true);
  
      mCategoryTV = rootView.findViewById(R.id.tv_category);
      mCategoryTV.setText(mCategory);
  
      mProgressBar = rootView.findViewById(R.id.progress_bar);
      mProgressBar.setVisibility(View.VISIBLE);
      mNoDataTV = rootView.findViewById(R.id.tv_no_data);
  
      return rootView;
  }
  </code></pre>
  
  <p>EDIT:
  crash logs:</p>
  
  <blockquote>
    <p>E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.thunderbolt.thebolt, PID: 31441
                                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
                                                                                 at com.thunderbolt.thebolt.ui.fragment.GalleriesFragment.fetchGalleriesOf(GalleriesFragment.java:146)
                                                                                 at com.thunderbolt.thebolt.ui.activity.MainActivity.onNavigationItemSelected(MainActivity.java:191)
                                                                                 at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:154)
                                                                                 at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                                 at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:89)
                                                                                 at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
                                                                                 at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
                                                                                 at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:342)
                                                                                 at android.view.View.performClick(View.java:5232)
                                                                                 at android.view.View$PerformClick.run(View.java:21289)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:168)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5885)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)</p>
  </blockquote>