Several things to understand here, first of all you are using
findViewById
, but in the
Fragment
derived class, secondly you are using it in the
onCreateView()
function. These are not allowed and you need to wait and call it from a later function (any other handler). A quick hack (and a bad practice) would be to think that you can just inflate a new view and capture the objects from there, just don't do that.
The best approach would be to call it any other function, such as
onStart()
function or any other function that gets executed when your View is shown to the user. It won't be a bad practice to show a "Loading..." to the user.
public void onStart() {
super.onStart();
TextView textView = (TextView) getView().findViewById(R.id.textView);
}
Notice the fact that onViewCreated() function is never guaranteed to be called thus this function would be useful. For more, you should consider reading the
Fragment
[
^] class reference to understand how functions get called — events get raised.
android - findViewById in Fragment - Stack Overflow[
^]