如何从SupportMapFragment获取片段扩展中的主Activity

时间:2016-02-04 20:18:40

标签: android android-fragments supportmapfragment

我有: 的 MainActivity

fragment = new ListReportsFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();

使用这个简单的代码,我调用从SupportMapFragment

扩展的framgment

MyFragment

public class MyFragment extends SupportMapFragment {
  public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MainActivity main = (MainActivity) getParentFragment().getActivity()
    }
}

main的值始终为null。我如何发送或获取MainActivity

1 个答案:

答案 0 :(得分:3)

更改

MainActivity main = (MainActivity) getParentFragment().getActivity();

MainActivity main = (MainActivity) getActivity();