Toggle Tabs in Viewpager via the Fragment button

Asked

Viewed 221 times

0

I wonder if there is any way to switch between tabs by the Ragment itself. I have a Viewpager with 2 tabs and I would like to switch via buttons because I validate some fields before switching tabs.

I disabled Swipe and also the action bar to prevent changing tabs without validating the output data. I didn’t post any code because I only need to know how to call another tab via button. the rest of the code is unnecessary I believe

 public void changePagerItem(int pageNumber) {
    mViewPager.setCurrentItem(1);
}

method implemented in the main class

btnFinalizar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ValidaPedidoAcp();
            ValidaPedidoObs();
            ValidaPedidoOpc();
            FastFoodPedidoActivity tabs = (FastFoodPedidoActivity)getActivity().getParent();
            tabs.changePagerItem(0);
        }
    });

method implemented in button.... but gives the following error

Process: com.meuapp.example, PID: 4048
     java.lang.NullPointerException: Attempt to invoke virtual method 'void Pedidos.FastFoodPedidoActivity.changePagerItem(int)' on a null object reference
         at Pedidos.Tabs.TabAdicionais$2.onClick(TabAdicionais.java:282)
         at android.view.View.performClick(View.java:6219)
         at android.view.View$PerformClick.run(View.java:24482)
         at android.os.Handler.handleCallback(Handler.java:769)
         at android.os.Handler.dispatchMessage(Handler.java:98)
         at android.os.Looper.loop(Looper.java:164)
         at android.app.ActivityThread.main(ActivityThread.java:6540)
         at java.lang.reflect.Method.invoke(Native Method)
         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  • If you want to know if "there is a way to switch between tabs by Fragment itself", the answer is yes.

1 answer

0


You’ll just need to define one method in his Activity that changes the Viewpager and then make a reference to their Activity of which the Fragment is attached and then you will have free access to method.

// ExampleActivity() ...
public void changePagerItem(Int pageNumber) {
    yourViewPager.setCurrentItem(page);
}

// Fragment() ...
((ExampleActivity) getActivity()).changePagerItem(3);
  • NullPointerException: Attempt to invoke virtual method 'void com.example.dev_03.sabbemobile.Telas.Pedidos.BarRestaurante.FastFood.FastFoodPedidos.FastFoodPedidoActivity.changePagerItem(int)' on a null object reference at Pedidos.Tabs.TabAdicionais$2.onClick(TabAdicionais.java:282)

  • gives this error, several methods of doing this are returning this error from null Object Reference

  • @Marciovieira, tried to ((FastFoodPedidoActivity)getActivity()).changePagerItem(1);?

  • yes, I tried.... same error until 0 is used

  • Do the following in the method changePagerItem print something random, if this occurs without errors, the problem is that 'mViewPager' is not being referenced, ie somehow it is null.

  • So, I was just changing the mSectionViewPager for the mViewPager that worked... Thanks for the help guys

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.