/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends
In this code when I am using TextView text=(TextView)getActivity().findViewById(R.id.text1)
Then my app crashes and Logcat says that Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
findViewById() on an Activity starts from the root view (what is referred to as the "decor view") and traverses the tree of available widgets and containers, looking for an ID match. Hence, findViewById() called on an Activity will only find views that are part of the overall activity view hierarchy.
However, your inflate() call does not attach the inflated RelativeLayout to the activity view hierarchy. This is expected. Those views will be attached to the activity view hierarchy when the fragment itself is attached, sometime after you return from onCreateView(). As a result, when you call findViewById() on the activity from inside onCreateView(), it cannot find your TextView, because that TextView is only being managed by the fragment and is not yet part of the activity view hierarchy.
findViewById() on a View or ViewGroup starts from that View or ViewGroup and traverses the tree of available widgets and containers, looking for an ID match. The RelativeLayout has access to your TextView, so when you call findViewById() on the RelativeLayout that you inflate, it works.
It's because fragment_detail.xml layout contains your TextView with id text1, not Activity layout.