Android : textView.setText not working

on Sunday, March 22, 2015


I have a TextView in a fragment called "frontpage". In my MainActivity (also called frontpage, and the fragment is also there), I want to change the text of my textview. Here is my code in the Mainactivity:


public static class PlaceholderFragment extends Fragment {



public PlaceholderFragment() {


}

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

}
public void setText(String text){
TextView textView = (TextView) getView().findViewById(R.id.NameView);
textView.setText("HI!");
}


}


And here is my code in the xml layout for the textview:



<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/NameView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />


No errors are being shown, but when running the app, the textview stays empty. Why is that?


0 comments:

Post a Comment