gpt4 book ai didi

android - SupportMapFragment 嵌套在另一个 fragment 中

转载 作者:行者123 更新时间:2023-11-29 01:51:13 25 4
gpt4 key购买 nike

我正在实现 Android 应用程序,我试图在另一个 fragment 中使用 SupportMapFragment。 map fragment 工作正常,我可以看到 map 并且没有错误。但是,在主要 Activity 中,我试图从嵌套 fragment 中获取 GoogleMap 对象,但我得到的只是空值。知道我该如何克服这个问题吗?

ma​​in_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />

fragment layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#E8E8E8">

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="117dp"
android:layout_margin="5dp" />
<LinearLayout/>

在 MainActivity.java::onCreate() 中

// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

//fm is null here

最佳答案

目前还不清楚您实际上这里有“嵌套 fragment ”。我只看到一个 fragment :SupportMapFragment

如果您的 ViewPager 在 fragment 内,并且您的 FragmentPagerAdapter 使用 SupportMapFragment,那么您需要调用 findFragmentById ()child FragmentManager 上,FragmentPagerAdapter 正在使用。

如果您的 ViewPager 不在 fragment 内,那么我不相信您有嵌套的 fragment 。但是,您的 ViewPager 可能尚未在整个 Activity 的 onCreate() 中实例化任何页面,因此您的 fragment 尚不存在。等到您的 FragmentPagerAdapter 创建了您的 SupportMapFragment然后尝试使用它。

关于android - SupportMapFragment 嵌套在另一个 fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17923311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com