问题描述
我在使用Google Maps时遇到问题,即getSupportFragmentManager().findFragmentById始终返回null.您有解决方法的想法吗?
I have a problem with Google Maps, i.e. getSupportFragmentManager().findFragmentById returns always null. Do you have an idea how to solve this?
这是代码:
fragment_map.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.something.MapFragment">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
MapsFragment.java:
MapsFragment.java:
public class MapFragment extends Fragment implements OnMapReadyCallback, android.location.LocationListener
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
...
我在活动"中使用了Google地图,并且可以使用以下代码:
I have Google Maps in Activity and it works with code:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
我正试图在片段中重用它,因为我需要在片段中而不是活动中使用地图,但这是行不通的.
I am trying to reuse this in fragment, becouse I need maps in fragment, not in activity, but it doesn't work.
我尝试过:
- 在"onCreateView"函数中调用此代码
-
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
-
GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
已过时,应用程序崩溃 -
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
和类似的变体,但在所有情况下,mapFragment都为空.
- calling this code in "onCreateView" function
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
is deprecated, and application crashesSupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
and similar variations, but in all cases I get null for mapFragment.
您知道我该如何解决这个问题?
Do you know how could I solve this problem?
推荐答案
问题是您正在尝试使用Activity的FragmentManager,而您应该使用Fragment的子FragmentManager.
The problem is that you're trying to use the Activity's FragmentManager, and you should be using the Fragment's child FragmentManager.
删除片段中的onCreate()
替代,并在填充布局并调用getMapAsync()
的位置添加onCreateView()
替代:
Remove the onCreate()
override in the Fragment, and add an onCreateView()
Override where you inflate the layout and call getMapAsync()
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return rootView;
}
这篇关于getSupportFragmentManager().findFragmentById对于Android片段中的Google地图返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!