NullPointerException calling activity using DialogFragment

前端 未结 2 1180
孤独总比滥情好
孤独总比滥情好 2021-01-16 16:55

I am in the process of converting my dialogs to DialogFragments. I\'m following the instructions here but it looks like they are using it in an Activity<

相关标签:
2条回答
  • 2021-01-16 17:30

    Shouldn't you use getSupportFragmentManager() and add (.add(fragment, tag)) the new instance of your fragment to the FragmentTransaction by doing .beginTransaction() and then call commit()? I guess thats what you are trying to do.

    getActivity() returns null since your new Fragment instance is not attached to the Activity (native FragmentActivity/Activity or SherlockFragmentActivity/SherlockActivity, whatever it is).

    Do some readings about using fragments. Instantiantion of the fragment is not enough. Find about underlying operations after instantiation.

    0 讨论(0)
  • 2021-01-16 17:45

    Use :

    getActivity().getApplicationContext()
    

    Instead of:

    getActivity()
    

    EDIT:

    Use this method

    @Override
    public void onAttach(SupportActivity activity) {
        super.onAttach(activity);
    }
    
    0 讨论(0)
提交回复
热议问题