0

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>
halfer
  • 19,824
  • 17
  • 99
  • 186
thelearner
  • 1,440
  • 3
  • 27
  • 58
  • You did add the respective `TabItem` in the layout XML? – Barns Dec 26 '17 at 21:53
  • I have added an xml snippet to the question. – thelearner Dec 26 '17 at 22:08
  • Have you set Tag while creating Tab like: tabLayout.addTab(tabLayout.newTab().setText("National").setTag(0)); if No the put tag to every new tab according to your sequence. May it can work. – Shahzad Afridi Dec 26 '17 at 22:12
  • TabLayout tabLayout = (TabLayout) findViewById(R.id.account_tabLayout); tabLayout.addTab(tabLayout.newTab().setText("SharkTab").setTag(0)); tabLayout.addTab(tabLayout.newTab().setText("MarkTab").setTag(1)); – Shahzad Afridi Dec 26 '17 at 22:16
  • (If you believe this is not a duplicate of the marked answer, you are free to explain why in the comments. It is not ideal to do so in the question, since our experience is most duplicate closures are correct, and meta-commentary tends to spoil the readability of questions that are not deleted.) – halfer May 12 '18 at 16:35

1 Answers1

5

You need to add the TabItems. You can do this in your Layout XML. Something like this:

<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>  

You should be able to give each a TabItem an id android:id="@+id/tabItemX1" and access it directly instead of using .getTabAt(0)

Barns
  • 4,850
  • 3
  • 17
  • 31