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.
– viana