英文:
How to use google maps android SDK in a fragment?
问题
我正在使用 Android 地图 SDK 并尝试在地图上绘制一个点。我按照这里给出的步骤进行操作。
我收到一个 NPE(空指针异常),指示我的 Support Map Fragment 为空。我猜测这是因为我在主要的 Fragment 中使用了视图绑定(View Binding),并将视图设置为 binding.getroot()。
声明代码如下:
SupportMapFragment supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_view);
supportMapFragment.getMapAsync(this::onMapReady);
XML 布局代码如下:
<androidx.fragment.app.FragmentContainerView
android:id="@+id/map_view"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="match_parent"/>
编辑:
与视图绑定无关。
英文:
I am using android maps sdk and trying to plot a point on it. I followed the steps as given here
I get an NPE saying that my Support Map Fragment is null. I am guessing this happens due to the fact that I am using view binding and setting the view as binding.getroot() in my main fragment.
The code where I declared this.
SupportMapFragment supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_view);
supportMapFragment.getMapAsync(this::onMapReady);
The XML where I have declared this.
<androidx.fragment.app.FragmentContainerView
android:id="@+id/map_view"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="match_parent"/>
EDIT:
Had nothing to do with View binding.
答案1
得分: 2
你应该使用 <fragment>
标签而不是 androidx.fragment.app.FragmentContainerView
。FragmentContainerView
只是一个容器,用于在运行时添加片段。另一方面,<fragment>
本身就是一个片段,这就是为什么有 android:name
属性。
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
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" />
如果你在使用一个片段,你应该使用 getChildFragmentManager()
。
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_view);
更新 -> 是的,抱歉我之前说错了。事实证明 FragmentContainerView
确实支持在 XML 中添加片段。尝试以下代码。
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/fragmentContainerView"
android:layout_width="match_parent"
android:tag="mapFragment"
android:layout_height="match_parent" />
然后你可以使用 #findFragmentByTag
来获取地图片段。
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentByTag("mapFragment");
英文:
You should be using <fragment>
tag not androidx.fragment.app.FragmentContainerView
. FragmentContainerView
is just a Container for fragments on which we can add fragments at runtime . on other hand <fragment>
is itself a fragment this why the android:name
attribute is there.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
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"
/>
If you using using it a fragment you should be using getChildFragmentManager()
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_view);
Update-> Yeah sorry i was wrong earlier . turns out FragmentContainerView
does support adding fragment in xml. Try the below code.
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/fragmentContainerView"
android:layout_width="match_parent"
android:tag="mapFragment"
android:layout_height="match_parent" />
Then you can get the map fragment by using #findFragmentByTag
.
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentByTag("mapFragment");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论