NullPointerException calling activity using DialogFragment

百般思念 提交于 2019-12-01 14:15:48

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.

Use :

getActivity().getApplicationContext()

Instead of:

getActivity()

EDIT:

Use this method

@Override
public void onAttach(SupportActivity activity) {
    super.onAttach(activity);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!