Android getTabAt()返回空对象

Android getTabAt()返回空对象,android,android-layout,android-fragments,nullpointerexception,android-tablayout,Android,Android Layout,Android Fragments,Nullpointerexception,Android Tablayout,一切都很好,我对这段代码从来没有任何问题。 但不知何故,当我尝试接收不在第一个位置的选项卡时,我现在得到一个空对象: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout$Tab.setCustomView(android.view.view)” 在onCreateView中调用TableO

一切都很好,我对这段代码从来没有任何问题。 但不知何故,当我尝试接收不在第一个位置的选项卡时,我现在得到一个空对象:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout$Tab.setCustomView(android.view.view)”

在onCreateView中调用TableOutsetup

 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)
)接收选项卡根本不是问题。但是,我无法在位置1接收选项卡

XML:


您需要添加
选项卡items
。您可以在布局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>  


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

您需要添加
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>  


您应该能够为每个a
TabItem
提供一个id
android:id=“@+id/tabItemX1”
,并直接访问它,而不是使用
.getTabAt(0)

您在布局XML中添加了相应的
TabItem
?我在问题中添加了一个XML片段。您是否在创建tabLayout.addTab(tabLayout.newTab().setText(“National”).setTag(0))等选项卡时设置了标记;如果没有,则根据您的顺序将标签放到每个新标签上。TabLayout=(TabLayout)findViewById(R.id.account\u TabLayout);tabLayout.addTab(tabLayout.newTab().setText(“SharkTab”).setTag(0));tabLayout.addTab(tabLayout.newTab().setText(“MarkTab”).setTag(1));(如果您认为这不是标记答案的重复,您可以在评论中自由解释原因。在问题中这样做并不理想,因为我们的经验是,大多数重复的闭包都是正确的,元评论往往会破坏未删除问题的可读性。)您是否在布局XML中添加了相应的
TabItem
?我在问题中添加了一个XML片段。您是否在创建选项卡时设置了标记,如:tabLayout.addTab(tabLayout.newTab().setText(“National”).setTag(0));如果没有,则根据您的顺序将标签放到每个新标签上。TabLayout=(TabLayout)findViewById(R.id.account\u TabLayout);tabLayout.addTab(tabLayout.newTab().setText(“SharkTab”).setTag(0));tabLayout.addTab(tabLayout.newTab().setText(“MarkTab”).setTag(1));(如果你认为这不是标记答案的重复,你可以在评论中自由解释原因。在问题中这样做并不理想,因为我们的经验是,大多数重复的闭包都是正确的,元评论往往会破坏未删除问题的可读性。)这是非常好的,可以使用它。这是一个非常好的工作。坦克你