我有: 的 MainActivity
fragment = new ListReportsFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();
使用这个简单的代码,我调用从SupportMapFragment
MyFragment
public class MyFragment extends SupportMapFragment {
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
MainActivity main = (MainActivity) getParentFragment().getActivity()
}
}
但main
的值始终为null
。我如何发送或获取MainActivity
?
答案 0 :(得分:3)
更改
MainActivity main = (MainActivity) getParentFragment().getActivity();
带
MainActivity main = (MainActivity) getActivity();