gpt4 book ai didi

android - SupportMapFragment 的 findFragmentById 在 Android Studio 中返回 null

转载 作者:IT王子 更新时间:2023-10-29 00:07:05 25 4
gpt4 key购买 nike

最近我将一个项目从 Eclipse 迁移到 Android Studio。除了我的一个使用 SupportMapFragment 的 fragment 外,一切都设置好了并且工作正常。下面的 findFragmentById(在 Eclipse 中构建时有效)现在返回 null :(

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SupportMapFragment m = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.safety_map));

xml fragment ...

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
android:id="@+id/safety_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="40dp"
map:cameraTargetLat="@string/livesafe_latitude"
map:cameraTargetLng="@string/livesafe_longitude"
map:uiZoomControls="false"
class="com.google.android.gms.maps.SupportMapFragment"/>

这是我在 build.gradle 中的依赖项:

 dependencies {
//google analytics
compile 'com.google.apis:google-api-services-analytics:v3-rev103-1.19.0'
//support library for api 10
compile 'com.android.support:support-v4:21.0.0'
//google play services
compile 'com.google.android.gms:play-services:6.1.11'
compile project(':facebook')
compile files('libs/android-support-multidex.jar')
compile files('libs/aws-android-sdk-1.6.0-debug.jar')
compile files('libs/FlurryAnalytics_3.3.2.jar')
}

我没有更改之前在 Eclipse 中工作的 xml 文件或 Fragment 类中的任何代码。

最佳答案

从您重写 Fragment.onActivityCreated() 的事实来看,我认为包含 map fragment 的布局就是您的 Fragment 的布局。在这种情况下,SupportMapFragment 是托管 Fragmentchild fragment 。当您尝试检索它时,您正在使用 Activity FragmentManager。您应该改用 FragmentFragmentManager:

例如,这个:

SupportMapFragment m = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.safety_map));

变成:

SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.safety_map));

关于android - SupportMapFragment 的 findFragmentById 在 Android Studio 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26597161/

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