The views you're attempting to find with findViewById()
are not yet in the activity view hierarchy. They are in the fragment which is not yet created. They are not in the activity layout set with setContentView()
. Therefore a null is returned and attempting to do something with the null causes an NPE.
The fragment transaction that creates the fragment view hierarchy and attaches it to the activity is usually run in onStart()
of the activity lifecycle. onStart()
comes after onCreate()
.
The tutorials are probably written without considering fragments and have the views in the activity layout directly.