This one is driving me nuts. Basically, I want to create a ViewPager
and add a few Fragment
s to it. Then, all I want to do, it set a value in one of th
The TextView
is located in the fragments layout, not in the ViewPagers
or the PagerAdapter
, that is causing the NPE. Now, you have 2 options.
Secondly, you could make the TextView
into FragmentA static, so it can be accessed by other classes. So your code would look something like this:
....
TextView myText;
@Override
public View onCreateView(....) {
myLayout = ....;
myText = myLayout.findViewById(yourID);
....
}
And then you would change the text from somewhere else (if it's really necessary):
FragmentA.myText.setText("new text");
Explaining method 2
Use the following in your Fragment.
public static void setText(String text) {
TextView t = (TextView) getView().findViewById(R.id.someTextView);
t.setText(text);
}
Then change the text like:
FragmentA.setText("Lulz");