I trying to write a code in Android, to switch from one tab to another tab by click on a button.
I know to by clicking on tab we can switch from one tab to another but can it be possible to switch from one tab to another tab by clicking on one button.
I tried the following tutorial.
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
In this tutorial i have edited the MovieFragment.java file in following manner.
MovieFragment.java
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.R;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MoviesFragment extends Fragment {
ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
viewPager.setCurrentItem(0);
}
});
return rootView;
}}
XML Code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#17df0d">
<android.support.v4.view.ViewPager
android:id="@+id/myfivepanelpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Design Movies Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:text="Click"
/>
</RelativeLayout>
I have created a button in the xml layout and trying switch to tab -1 but i am getting Null Pointer Exception.
Error Code:
05-20 09:07:10.995: E/AndroidRuntime(1117): FATAL EXCEPTION: main
05-20 09:07:10.995: E/AndroidRuntime(1117): java.lang.NullPointerException
05-20 09:07:10.995: E/AndroidRuntime(1117): at info.androidhive.tabsswipe.MoviesFragment$1.onClick(MoviesFragment.java:33)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.view.View.performClick(View.java:4240)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.view.View$PerformClick.run(View.java:17721)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.os.Handler.handleCallback(Handler.java:730)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.os.Handler.dispatchMessage(Handler.java:92)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.os.Looper.loop(Looper.java:137)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-20 09:07:10.995: E/AndroidRuntime(1117): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 09:07:10.995: E/AndroidRuntime(1117): at java.lang.reflect.Method.invoke(Method.java:525)
05-20 09:07:10.995: E/AndroidRuntime(1117): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-20 09:07:10.995: E/AndroidRuntime(1117): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-20 09:07:10.995: E/AndroidRuntime(1117): at dalvik.system.NativeStart.main(Native Method)
Please suggest me some solution or some good technique to do it.
You have to call parent view inside your fragment.
public class MoviesFragment extends Fragment {
ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
viewPager.setCurrentItem(0);
}
});
return rootView;
}
}
your ViewPager is null because you are not initializing the variable.
You need do
public class MoviesFragment extends Fragment {
ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
viewPager = (ViewPager) rootView.findViewById(R.id.some_viewpager);//Initializing
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
viewPager.setCurrentItem(0);
}
});
return rootView;
}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With