Android : Support MapFragment Returns null

删除回忆录丶 提交于 2019-12-08 04:26:46

问题


I have updated the android SDK to 5.0 and my previous project is not working on it(which worked correctly in 4.4.2).

I have an class which extends fragment

public class MapFragment extends Fragment{
//
}

In this class i have inflated a layout with google support mapfragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_tile_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible" >

    <RelativeLayout
        android:id="@+id/full_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />

        <Textview
            <!-- dfh
            fgh -->
        />

    </RelativeLayout>

</RelativeLayout>

When i do this in my code it return null

    SupportMapFragment fragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

This same line worked for me in Android4.4.2 but after updation there is some problem...

Do anyone found the solution...


回答1:


I've found the answer and it works for me

Instead of this

 SupportMapFragment fragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

I've changed as

SupportMapFragment  fragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);


来源:https://stackoverflow.com/questions/26484217/android-support-mapfragment-returns-null

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