【问题标题】:Mapfragment findFragmentById always nullMapfragment findFragmentById 始终为空
【发布时间】:2013-06-02 18:29:41
【问题描述】:

我无法访问地图的片段。 getFragmentManager().findFragmentById(R.id.map)) 总是返回 null。 我不知道为什么。 有什么问题?

谢谢!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <TextView
            android:id="@+id/tvNombreCentro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />


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

    </LinearLayout>

在 setContentView 之后的活动中,我尝试访问地图,但收到异常

public class Mapa extends Activity {
private GoogleMap mMap;
private ActionBar ab;

private TextView tvNombreCentro;
private TextView tvTelefonoValor;
private TextView tvEMailValor;
private TextView tvWebValor;
private TextView tvDireccionValor;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mapa_centro);
    GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
}

【问题讨论】:

    标签: android google-maps fragment mapfragment


    【解决方案1】:

    这样使用:

    SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
    
                map = mapFrag.getMap();
    

    here is full code with sample:

    【讨论】:

    • 请注意,如果您使用 Android 支持库来显示地图,请确保您的包含地图片段的布局 xml 文件的 android:name 属性值应为 com.google.android.gms.maps.SupportMapFragment 而不是com.google.android.gms.maps.MapFragment,否则将无法正常工作并继续返回null
    • 太棒了!它对我有帮助!。
    【解决方案2】:

    如果你的问题没有解决,试试这个:

    当您使用SupportMapFragment 时,在检索地图时,请使用

       GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    

    另外,改变

       public class Mapa extends Activity {
    

       public class Mapa extends FragmentActivity {
    

    【讨论】:

    • 抱歉耽搁了。答案是正确的,但我不需要更改扩展,只需要更改 getSupportFragmentManager。谢谢!
    【解决方案3】:

    我在尝试获取 MapFragment 时遇到了类似的问题,但由于设备没有更新 Google Play 服务而得到 null。

    【讨论】:

      【解决方案4】:

      布局变化

      android:name = "com.google.android.gms.maps.SupportMapFragment"

      android:name = "com.google.android.gms.maps.MapFragment".

      或者,您可以使用getSupportFragmentManager() 代替getFragmentManager()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-31
        • 2019-07-18
        • 2019-08-18
        • 2011-12-29
        • 2019-07-02
        • 2019-05-04
        • 1970-01-01
        相关资源
        最近更新 更多