First method: You're calling findViewById()
when the activity is instantiated and its member variables are initialized. The activity won't have a Window
yet and it will NPE. Call findViewById()
only in onCreate()
or later, after setContentView()
.
Second method: You don't have setContentView()
with a view hierarchy that has a view with the given id. null
is returned and invoking method on it will NPE.
From the code you later added, it seems the view is in your fragment layout and not the activity layout. It won't be a part of the activity view hierarchy in onCreate()
. You should move the code to the fragment's onCreateView()
instead:
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
contactBadge = (QuickContactBadge) rootView.findViewById(R.id.quickContactBadge);
contactBadge.assignContactFromEmail("pointlightproductions.net@gmail.com", true);