I cannot seem to change the text in a textview that is in an fragment from my fragmentActivity. Additionally I have tried frag.getView().findViewById
which while I would think is the right way to do it, returns null and throws a nullpointer exception.
If I write methods in the Fragment itself to set the text and use getView(), I also get nullpointer exceptions.
Also of note, the first fragment loaded, called by the fragment:name, loads and populates text with this same code. It only does not work after FragmentTransaction.replace()
has been called
The code below does not throw any exceptions, however the text remains blank.
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getTag().equals("info")) {
DisplayEventInfoFragment frag = new DisplayEventInfoFragment();
ft.replace(R.id.detailsfragment, frag);
final TextView mTimeView = (TextView)findViewById(R.id.ievent_title);
final TextView mVenueView = (TextView)findViewById(R.id.ievent_venue);
final TextView mDescView = (TextView)findViewById(R.id.ievent_description);
mTimeView.setText(thisEvent.get("theTime")+" - "+thisEvent.get("endTime"));
mVenueView.setText(thisEvent.get("venue"));
mDescView.setText(thisEvent.get("description"));
} else if(tab.getTag().equals("location")) {
//DisplayEventMapFragment frag = new DisplayEventMapFragment();
//ft.replace(R.id.detailsfragment, frag);
}
}
When querying the main View or any sub Views of a Fragment, you must wait until Fragment.onCreateView() has been called.
It seems odd that the parent Activity is responsible for modifying layout components of Fragments. One of the main advantages of using Fragments is that they can be self contained and reused throughout your application.
Perhaps a more viable approach would be to override onActivityCreated() or onStart() of your Fragments (which are called after onCreateView()), and have them populate their TextViews there.