Really simple but just not getting it.
I want to reference a view by ID that is in the Fragments XML File that I inflate.
I’m declaring the view.
TextView nameView;
I’m getting the view from the layout.
View view = inflater.inflate(R.layout.trackfrag, group, false);
nameView = (TextView)view.findViewById(R.id.trackName);
I get the problem in the below method when it is called from its activity.
public void setTrackDetails (String name, String description) {
Log.e("TRACKFRAG", "setTrackDetails Called");
nameView.setText(name);
Log.e("TRACKFRAG", "setTrackDetails Finished");
}
Any help would be much appreciated.
ADDED XML
<TextView
android:id="@+id/trackName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/anchor"
android:text="Track Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="22sp" />
The Fragment loads up and is displayed fine if I don’t call this method but I need a way of changing the text in one of the Text Views.
ADDED FRAGMENT CREATION
public void onTrackSelected(String track) {
Log.e("FRAGTRANS","History List Pressed");
Fragment trackFrag = getFragmentManager().findFragmentByTag("historytrack");
if (trackFrag == null) {
trackFrag = new TrackFragment();
Log.e("TRACKSEL","new trackfrag created");
}
addFragment(trackFrag, true, HISTORYTAG, FragmentTransaction.TRANSIT_NONE);
((TrackFragment) trackFrag).setTrackDetails(track,"testing");
}
ADD FRAGMENT METHOD
void addFragment(Fragment fragment, boolean addToBackStack, String tag, int transition) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, fragment);
ft.setTransition(transition);
if (addToBackStack)
ft.addToBackStack(null);
ft.commit();
}
Your problem is in invoking setTrackDetails() method. Please look here where you can see that:
So just after you invoke commit your fragment is not created yet or creating is in progress and your nameView isn’t set yet.
Best solution would be after creating your fragment(can be after setting nameView because fragment already has activity) invoking method from activity which takes text:
Please read documentation about fragments and its lifecycle.