I want to create a tablayout inside a cardview. But when I tried adding tabs to it dynamically , it gives me the following error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout$TabView.setContentDescription(java.lang.CharSequence)' on a null object reference
I have searched many stackoverflow posts but Im unable to resolve this issue.To mention, I dont have appbarlayout and tablayout is not directly below the toolbar, rather inside a cardview.
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:background="@color/blue"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/toolbar"
>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="0dp"
app:cardCornerRadius="18dp"
android:elevation="10dp"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<include
android:layout_margin="15dp"
layout="@layout/l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
></include>
<include
layout="@layout/layout1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="130dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/l2"
></include>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/l2"
android:id="@+id/tablayout"
app:tabGravity="fill"
app:tabMode="fixed"
></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/tablayout"
android:id="@+id/viewPager"
>
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
Here, the layouts l1 and l2 are visible and when I tried to dynamically create tabs, it crashes the app.
Below is my MainActivity:
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.tablayout)
TabLayout tabLayout;
@BindView(R.id.viewPager)
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_xml);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
List<String> title = new ArrayList<>();
title.add(TAB1TEXT);
title.add(TAB2TEXT);
title.add(TAB3TEXT);
title.add(TAB4TEXT);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(),title);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
tabLayout.addTab(new TabLayout.Tab().setText(TAB1TEXT));//CRASHES HERE
tabLayout.addTab(new TabLayout.Tab().setText(TAB2TEXT));
tabLayout.addTab(new TabLayout.Tab().setText(TAB3TEXT));
tabLayout.addTab(new TabLayout.Tab().setText(TAB4TEXT));
}
It actually crashes at this line: tabLayout.addTab(new TabLayout.Tab().setText(TAB1TEXT))
Edit: The tabLayout instance variable,where the crash happens is not null and im not sure what is actually causing this NPE, may be something inside goes out for a toss!
Any help or insights will be really helpful.Thanks in advance!