I try to pass arguments one to another tab in View pager.But value not pass.I have write code in onTabSelected() function.but, Only Null value occurred.Please help me.follow my code.
MainActivity
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
//actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
Bundle bundle=new Bundle();
bundle.putString("name", "From Activity");
//set Fragmentclass Arguments
MoviesFragment fragobj=new MoviesFragment();
fragobj.setArguments(bundle);
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
MoviesFragments
public class MoviesFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext=getArguments().getString("name"); // ERROR in NULLPointerExecption
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
TextView text=(TextView) rootView.findViewById(R.id.text1);
text.setText("Movie");
return rootView;
}
}
I try to this code but strtext error occured in NullPointerException
You need to do something like that: