用于 SupportMapFragment 的 findFragmentById 在 Android Studio 中返回 null [英] findFragmentById for SupportMapFragment returns null in Android Studio

查看:29
本文介绍了用于 SupportMapFragment 的 findFragmentById 在 Android Studio 中返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Recently I migrated a project from Eclipse to Android Studio. Everything is setup and working fine except for my one fragment which uses a SupportMapFragment. The below findFragmentById (which worked when building in Eclipse) is now returning null :(

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

xml 片段...

<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 中的依赖项:

Here are my dependencies in my 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 类中的任何代码.

I haven't changed any code in the xml file or the Fragment class that previously worked in Eclipse.

推荐答案

根据您覆盖 Fragment.onActivityCreated() 的事实判断,我认为您的包含地图片段的布局是Fragment 的布局.在这种情况下,SupportMapFragment 是您的托管 Fragment 片段.当您尝试检索它时,您正在使用 Activity FragmentManager.你应该改用你的 FragmentFragmentManager:

Judging by the fact that you're overriding Fragment.onActivityCreated(), I take it that your layout containing the map fragment is the layout for your Fragment. In that case, the SupportMapFragment is a child fragment of your hosting Fragment. When you attempt to retrieve it, you're using the Activity FragmentManager. You should instead use your Fragment's FragmentManager:

例如:

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

变成:

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

这篇关于用于 SupportMapFragment 的 findFragmentById 在 Android Studio 中返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆