Everything worked fine and I never had any problems with this code. But somehow I now get a null object when I try to receive a Tab that is not in the first position:
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 is called in 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);
It's really strange because it worked smoothly before. Receiving the Tab at the position 0 (.getTabAt(0)
) is not a problem at all. However, I cannot receive the Tab at the position one.
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>