pass data from one swipe tab to another Null Value present?

446 views Asked by At

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

1

There are 1 answers

7
yshahak On

You need to do something like that:

 @Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view

    //set Fragmentclass Arguments
    MoviesFragment preInitializedFragment = (MoviesFragment) getSupportFragmentManager().findFragmentByTag(MoviesFragment.class.getName());
    if (preInitializedFragment == null) {
        Bundle bundle=new Bundle();
        bundle.putString("name", "From Activity");
        MoviesFragment mFragment=new MoviesFragment();
        mFragment.setArguments(bundle);
        ft.add(android.R.id.content, mFragment, MoviesFragment.class.getName());
    } else {
        ft.attach(preInitializedFragment);
    }

}