This is because findViewById() searches in the activity_main layout, while the button is located in the fragment's layout fragment_main.