1

hey I am trying to use tabs on android but i am getting error when setting the tablayout

Here is my code

TabLayout tabLayout = findViewById(R.id.tablayout);
    tabLayout.addTab(tabLayout.newTab().setText("Fruits"));
    tabLayout.addTab(tabLayout.newTab().setText("Vegetables"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


    final ViewPager viewPager = findViewById(R.id.pager);


    final FruitsAdapter adapter = new FruitsAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(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) {

        }
    });

this is the error

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mohad.grocery/com.mohad.grocery.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout.newTab()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout.newTab()' on a null object reference
    at com.mohad.grocery.Main2Activity.onCreate(Main2Activity.java:30)

I couldn't find anyone who has the same problem as me so I asked his question

The problem is on newtab but I dont know why

Thank you for help

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
Mohad12211
  • 353
  • 3
  • 9

2 Answers2

1

When you do TabLayout tabLayout = findViewById(R.id.tablayout);, tabLayout is null. The problem is probably because you referencing a wrong TabLayout id in your layout file (.xml).

Please validate that both id's are the same (case sensitive).

Daniel Beleza
  • 389
  • 1
  • 15
0

I think you are missing a call to setupWithViewPager() after initializing the viewPager.

Can you try?

    final FruitsAdapter adapter = new FruitsAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
Hichem BOUSSETTA
  • 1,791
  • 1
  • 21
  • 27