-1

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!

adi
  • 598
  • 8
  • 25
  • Add setContentDescription("some description"); on TabView. – Rahul Singh Aug 27 '19 at 15:39
  • It crashes again with the same exception at same place! – adi Aug 27 '19 at 15:42
  • the tablayout instance over here is not null and for that reason only I have posted question here.moreover i have clearly indicated where am I actually facing this error! kindly have a look at the question before downvoting ! – adi Aug 27 '19 at 17:52
  • I have checked many times, its not null, i removed butterknife and used findviewbyid as well.but its not working! – adi Aug 28 '19 at 10:03
  • My apologies. I misread the Exception message. Instead of instantiating the `Tab`s yourself, you need to let the `TabLayout` do it. That is, change `new TabLayout.Tab()` to `tabLayout.newTab()`. For example, in the crashing line: `tabLayout.addTab(tabLayout.newTab().setText(TAB1TEXT));`. – Mike M. Aug 28 '19 at 11:12
  • Hi Mike, thank you for the input, but sorry to tell , it didn't work ..i have already tried it before – adi Aug 28 '19 at 11:15
  • Hmm, I can find a version of `TabLayout` where that seems possible, with `newTab()`. In the cases where it might be possible, it should be throwing an Exception earlier. Which version of the design library are you using? (Btw, have you considered upgrading to androidx?) Also, could you please provide the complete stack trace? – Mike M. Aug 28 '19 at 11:32
  • im not using androidx, bcoz its a big codebase so kept it for later.i have removed the build tools version as such, bcoz android studio gave me warning, so i guess, it would take the most recent version which is 28.0.2 – adi Aug 28 '19 at 11:37
  • Well, looking at the source, with `tabLayout.newTab()`, it should be crashing before the `setContentDescription()` call in the posted Exception message. Are you sure the stack trace isn't different when you use `tabLayout.newTab()` instead? – Mike M. Aug 28 '19 at 11:50

1 Answers1

0

don't use @BindView, just make global variable like

TabLayout tabs;

then, inside @OnCreate after .setContentView();

tabs = (TabLayout)findViewbyId(R.id.tablayout);

it's better always have a single variable for each element on the view who will change or will be seeting data, just do it like that, the activity couldn't find t¿the specific view. let me know how it goes