TabLayout.getTabAt() returns 空对象

TabLayout.getTabAt() returns null object

一切正常,我在使用这段代码时从未遇到过任何问题。 但是不知何故,当我尝试接收不在第一个位置的 Tab 时,我现在得到一个空对象:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout$Tab.setCustomView(android.view.View)' on a null object reference

tabLayoutSetup 在 onCreateView 中调用。

 private void tabLayoutSetup(){
    // Get the ViewPager and set it's PagerAdapter so that it can display items
    Bugger.t(context,"tablayout");
    viewPager.setAdapter(pagerAdapter);
    // Give the TabLayout the ViewPager
    tabLayout.setupWithViewPager(viewPager);
     createTabs();

}

//this method is creating all tabs for account's tablayout
private void createTabs(){
    TabLayout.Tab ShareTab = tabLayout.getTabAt(0);
    TabLayout.Tab MarkTab = tabLayout.getTabAt(1);

    //***********************************************************************************************************
    //inflating template for first tab, Activities.
    View TabView = LayoutInflater.from(context).inflate(R.layout.account_tablayout_customtab, null); //template
    TextView titel = (TextView) TabView.findViewById(R.id.tab_titel);
    //populating first template for 'Activities'
    titel.setText(getResources().getString(R.string.SharesTab));
    titel.setTextColor(ContextCompat.getColor(context,R.color.tabLayout_active));
    ShareTab.setCustomView(TabView);

    //**********************************************************************************************************
    //inflating template for second tab, Messages.
    TabView = LayoutInflater.from(context).inflate(R.layout.account_tablayout_customtab, null); //template
    titel = (TextView) TabView.findViewById(R.id.tab_titel);
    //populating second template for 'Messages'
    titel.setText(getResources().getString(R.string.MarkTab));
    MarkTab.setCustomView(TabView);

真的很奇怪,因为它之前运行顺利。在0(.getTabAt(0))位置接收Tab完全没有问题。但是,我收不到第一个位置的Tab

XML:

<android.support.design.widget.TabLayout
            android:id="@+id/account_tabLayout"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_marginTop="12dp"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/account_bio">


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

您需要添加 TabItems。您可以在布局 XML 中执行此操作。像这样:

<android.support.design.widget.TabLayout
            android:id="@+id/account_tabLayout"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_marginTop="12dp"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/account_bio">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button_menu"
                />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button_favorites"
                />


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

您应该能够给每个 TabItem 一个 ID android:id="@+id/tabItemX1" 并直接访问它而不是使用 .getTabAt(0)