4

I have an Activity with a ViewPager containing multiple fragments. how can i now access a TextView in one of that fragments to change its text from the main activity? I tried multiple ways and they all ended in a NullPointerException.

Activity:

public class SummonerOverview extends SherlockFragmentActivity implements TabListener, OnPageChangeListener {


    private ViewPager mPager;
    private PagerAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.summoner_overview);

        initialize();   
    }

    private void initialize() {

        // initialize Pager
        mPager = (ViewPager) findViewById(R.id.viewpager);
        mAdapter = new PagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mAdapter);
        mPager.setCurrentItem(1);
        mPager.setOnPageChangeListener(this);


        }
}

PagerAdapter:

public class PagerAdapter extends FragmentPagerAdapter {

    public PagerAdapter(FragmentManager fm) {
        super(fm);
        frags = new Fragment[3];
        frags[0] = new StatisticsFragment(0);
        frags[1] = new RatingsFragment(1);
        frags[2] = new HistoryFragment(2);
    }

    private final int NUM_PAGES = 3;
    Fragment[] frags;

    @Override
    public Fragment getItem(int arg0) {
        if (arg0 == 0)
            return frags[0];
        else if (arg0 == 1)
            return frags[1];
        else
            return frags[2];
    }
    @Override
    public int getCount() {
        return NUM_PAGES;
    }

}

The Fragment:

public class StatisticsFragment extends SherlockFragment {

public StatisticsFragment(int fragNr) {
        this.fragNr = fragNr;
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_overview_statistics, container, false);

        return v;
    }

}

The Textview in the StatisticsFragment is labeled with an id in the fragment_overview_statistics.xml, but when i try

TextView tv = (TextView) findViewById(R.id.id_of_the_textview)
tv.setText("text");

from within the onCreate() Method of the Activity after the initialize() Method, i get an Exception.

danijoo
  • 2,823
  • 4
  • 23
  • 43
  • 1
    Trying to look for that `TextView` in the `onCreate` method(which you shouldn't do) in the first place is incorrect as the `Fragments` will not be available there. If you need to set the text at that moment then move the setting of the text directly in the fragment class. – user Mar 09 '13 at 13:22
  • When is the first time the Fragment (and therefore the tv) is available? After the activity is full running (after onResume)? Maybe its just a designproblem of me. I want to display some Data I fetched from the internet. I start with creating the Activity, Viewpager and Fragments. After that, i start an AsyncTask to fetch the Data from the internet and write the parsed Data to to TextViews in the onPostExecute() Method of the AsyncTask. – danijoo Mar 09 '13 at 13:36
  • 1
    If that is what you're doing I would go with the solution in your answer. As you require to set the `TextView` for all(I presume) fragments just rebuild them. In the `onResume` method the fragments should be available. – user Mar 09 '13 at 13:57

1 Answers1

0

Okay, after another hour of googling, i think i solves my own question (although i'm sure there is a better method to solve that)

I now hold all the Data that is displayed by the fragments in the MainActivity and make the Fragmets consume this Data in their onCreateView() Method by using public Methods of the Activity.

In the PagerAdapter, i overwrote getItemPosition() to:

public int getItemPosition(Object object) {
    return POSITION_NONE;
}

Now, everytime i update some of the Data, i also call mAdapter.notifyDataSetChanged() which leads into a recreation of all fragments.

It works, although it looks like a poor hack for me. Im sure there must be a better solution because now i have to recreate all fragments for changing one TextView of one Fragment, what is surely not the way it is intended to be done.

danijoo
  • 2,823
  • 4
  • 23
  • 43