用于 SupportMapFragment 的 findFragmentById 在 Android Studio 中返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26597161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
findFragmentById for SupportMapFragment returns null in Android Studio
提问by J_Sizzle
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 :(
最近我将一个项目从 Eclipse 迁移到 Android Studio。除了我的一个使用 SupportMapFragment 的片段外,一切都设置好并且工作正常。下面的 findFragmentById(在 Eclipse 中构建时有效)现在返回 null :(
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SupportMapFragment m = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.safety_map));
snippet of xml...
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"/>
Here are my dependencies in my build.gradle:
这是我在 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')
}
I haven't changed any code in the xml file or the Fragment class that previously worked in Eclipse.
我没有更改 xml 文件或以前在 Eclipse 中工作的 Fragment 类中的任何代码。
回答by Kevin Coppock
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 childfragment of your hosting Fragment
. When you attempt to retrieve it, you're using the Activity
FragmentManager
. You should instead use your Fragment
's FragmentManager
:
从您覆盖的事实来看Fragment.onActivityCreated()
,我认为包含地图片段的布局是您的Fragment
. 在这种情况下,SupportMapFragment
是您托管的子片段Fragment
。当您尝试检索它时,您正在使用Activity
FragmentManager
. 您应该改用您的Fragment
's FragmentManager
:
For example, this:
例如,这个:
SupportMapFragment m = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.safety_map));
becomes:
变成:
SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.safety_map));
回答by cosic
Yes. It's not work for targetSdkVersion 21, if your Map fragment is inner part of Fragment (due to some issues, that mentioned thisand this).
是的。它不适用于 targetSdkVersion 21,如果您的 Map 片段是 Fragment 的内部部分(由于某些问题,提到了this和this)。
As temporary resolving can advice such trick:
由于临时解决可以建议这样的技巧:
public class MyFragment extends Fragment {
private SupportMapFragment fragment;
private GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_with_map, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map_container, fragment).commit();
}
}
}
回答by T. pac
The following code worked for me. I was using Google Map in a Fragment
:
以下代码对我有用。我在以下位置使用 Google 地图Fragment
:
SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager() .findFragmentById(R.id.safety_map));
回答by Parsania Hardik
All the answers are correct in their respective manner. It may happen that you follow someones's java code which is working for them but not work for you because you had different implementation in your xml code than them.
所有答案在各自的方式中都是正确的。您可能会遵循某人的 java 代码,这些代码对他们有用但对您不起作用,因为您的 xml 代码中的实现与他们不同。
So let me give you my opinion how I managed to load map in both Activity
as well as Fragment
所以让我给你我的意见,我如何设法加载地图Activity
以及Fragment
Activity:
活动:
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Java class:
Java类:
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
In Fragment:
在片段中:
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Java class:
Java类:
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);