Android SearchView and TabLayout view is null after rotation and recreation
<p>I'm using android.support.design.widget.TabLayout for tabbed view. Below some part of my code. Problem is that after rotating screen list is recreated correctly but when I press search button on SearchView then listFrag.onSearchTextEntered is called and I get Exception that tvEmptyView is null:</p>

<pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
    at com.app.ListFrag.onSearchTextEntered(ListFrag.java:117)
    at android.support.v7.widget.SearchView.onTextChanged(SearchView.java:1150)
    at android.support.v7.widget.SearchView.access$2000(SearchView.java:103)
    at android.support.v7.widget.SearchView$12.onTextChanged(SearchView.java:1680)
    ..................
</code></pre>

<p>Line 117 is "tvEmptyView.setVisibility(View.GONE);"
But how is it possible that tvEmptyView is null if it is found in onCreateView method?? Before rotating SearchView is working fine and tvEmptyView is not null!</p>

<pre><code>public class MainActivity extends AppCompatActivity {
ListFrag listFrag;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
    ViewPager tabPager = (ViewPager)findViewById(R.id.tabPager);

    setSupportActionBar(toolbar);

    tabAdapter = new ViewPagerAdapter(getSupportFragmentManager());
    MapFrag mapFrag = new MapFrag();
    listFrag = new ListFrag();
    tabAdapter.addFragment(mapFrag, "MAP");
    tabAdapter.addFragment(listFrag, "LIST");
    tabPager.setAdapter(tabAdapter);
    tabPager.setOffscreenPageLimit(3);
    tabLayout.setupWithViewPager(tabPager);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) myActionMenuItem.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
        listFrag.onSearchTextEntered(query);
        return false;
        }
        @Override
        public boolean onQueryTextChange(String query) {
        listFrag.onSearchTextEntered(query);
        return false;
        }
    });
    return true;
}

}
</code></pre>

<p>List Fragment Code:</p>

<pre><code>public class ListFrag extends Fragment {
RecyclerView recyclerView;
TextView tvEmptyView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.list_fragment, container, false);        
    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    tvEmptyView = (TextView) view.findViewById(R.id.tvEmptyView);
    createRecyclerView();
    return view;
}
private void createRecyclerView() {
    listAdapter = new MyListAdapter(getActivity(), new ArrayList&lt;MyData&gt;(), this);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(listAdapter);
}    
public void onSearchTextEntered(String text) {
    //filter data basing on entered text
    //............................
    if (listAdapter == null)
    createRecyclerView();
    listAdapter.setList(busStops);
    if (busStops.size() == 0)
    tvEmptyView.setVisibility(View.VISIBLE);
    else
    tvEmptyView.setVisibility(View.GONE);
}
}
</code></pre>

<p>list_fragment.xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" &gt;

    &lt;android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" /&gt;

    &lt;TextView
        android:id="@+id/tvEmptyView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/no_results"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:visibility="gone" /&gt;

&lt;/FrameLayout&gt;
</code></pre>

<p>A tried to find TextView inside on onSearchTextEntered for test:</p>

<pre><code>tvEmptyView = (TextView) getView().findViewById(R.id.tvEmptyView);
</code></pre>

<p>and I get other Exception that getView() is null!! I don't know what's going on... how getView() may be null if view and list (recycler view) if displayed ok??</p>