取自
http://developer.android.com/guide/topics/ui/settings.html:
Note: A PreferenceFragment doesn’t have a its own Context object. If
you need a Context object,you can call getActivity(). However,be
careful to call getActivity() only when the fragment is attached to an
activity. When the fragment is not yet attached,or was detached
during the end of its lifecycle,getActivity() will return null.
如果我从PreferenceFragment的OnCreate()方法中调用getActivity(),那么我可以放心,片段是附加到它的活动的 – 还有其他方法你应该得到这个实例中的Context?
我需要上下文的原因是我正在尝试使用PreferenceFragment中的Toast通知