Why is my context in my Fragment null?

前端 未结 4 1352
耶瑟儿~
耶瑟儿~ 2020-12-07 01:04

I have got a question regarding the usage of context in a fragment. My problem is that I always get a NullpointerException. Here is what i do:

Create a class that ex

4条回答
  •  天涯浪人
    2020-12-07 02:05

    When are you instantiating your Helper class? Make sure it's after onActivityCreated() in the lifecycle of the Fragment.

    http://developer.android.com/images/fragment_lifecycle.png

    The following code should work:

    @Override
      public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        helper = new Helper(getActivity());
      }
    

提交回复
热议问题