2

In meinem Projekt erstelle ich meine tabBar über ViewPager wie folgt aus:Android Entfernen tabBar (ausblenden) dynamisch

MainActivity.java

mViewPager = (ViewPager) findViewById(R.id.content_main_viewpager); 

     if (mViewPager != null) { 
      final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 

      adapter.addFragment(new LandingSearch(), ""); 
      adapter.addFragment(new LandingWiki(), ""); 
      adapter.addFragment(new LandingForum(), ""); 

      mViewPager.setAdapter(adapter); 
      mViewPager.setOffscreenPageLimit(3); 

      final TabLayout tabs = (TabLayout) findViewById(R.id.content_main_tabs); 

      if (tabs != null) { 
       tabs.post(new Runnable() { 
        @Override 
        public void run() { 
         tabs.setupWithViewPager(mViewPager); 
         tabs.getTabAt(0).setIcon(R.drawable.ic_tab_search); 
         tabs.getTabAt(1).setIcon(R.drawable.ic_tab_favorite); 
         tabs.getTabAt(2).setIcon(R.drawable.ic_tab_forum); 
        } 
       }); 

      } 
     } 

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:fitsSystemWindows="true" 
tools:context="com.mathleaks.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 

<com.mathleaks.ui.util.MainViewPager 
    android:id="@+id/content_main_viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:visibility="visible" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 


</com.mathleaks.ui.util.MainViewPager> 

<android.support.design.widget.TabLayout 
    android:id="@+id/content_main_tabs" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabMode="fixed" 
    app:tabGravity="center" 
    android:animateLayoutChanges="true" 
    android:background="@color/background" 
    app:background="@color/background" 
    app:layout_scrollFlags="scroll|enterAlways" > 

</android.support.design.widget.TabLayout> 

</LinearLayout> 

Jetzt möchte ich meine TabBar zur Laufzeit in meinem 2. TabBar-Fragment (LandingWiki) verstecken, wenn ein spezielles Ereignis in der App auftritt . Bisher war ich nicht erfolgreich.

final TabLayout tabs = (TabLayout)findViewById(R.id.content_main_tabs); 
tabs.setVisibility(View.GONE); 

Ergebnisse in:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.setVisibility(int)' on a null object reference 

Antwort

3

ist dies wahrscheinlich durch die Tatsache, dass findViewById an das Fragment statt Haupttätigkeit bezieht.

Sie können folgendes tun: aus Ihrem Fragment (nach dem onActivityCreated Ereignis)

final TabLayout tabs = (TabLayout)getActivity().findViewById(R.id.content_main_tabs); 
tabs.setVisibility(View.GONE);