如何修复supportFragmentManager?

问题描述 投票:-1回答:1

我试图通过以下方式将Google地图放入SupportMapFragment文件中的MainActivity

val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
        mapFragment?.getMapAsync(this)

val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)

并且像修改我的content_main.xml

 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:gravity="center_vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name"
                    android:textColor="#FFFFFF"
                    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
            </LinearLayout>

<LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="13"
                android:orientation="horizontal"
                android:gravity="center_vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name"
                    android:textColor="#FFFFFF"
                    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
            </LinearLayout>

但是该应用程序的工作方式如下:Instead map should be displayed in white area

有什么建议吗?

编辑这是我的Activity_Main.xml

<include
        xmlns:tools="http://schemas.android.com/tools"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        layout="@layout/content_main"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

<!--<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />-->

ps。我删除了<fragment>标签以调试某些部分。

android kotlin supportmapfragment child-fragment
1个回答
0
投票

我解决了;当大家在评论中嘲笑我时,如果有人遇到相同的情况,我将发布以供将来参考:

val mapFragment = SupportMapFragment.newInstance()
        supportFragmentManager.beginTransaction().add(R.id.map, mapFragment)
            .commit()
        mapFragment.getMapAsync(this)

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.