我在Fragment中实现Google Map时遇到了麻烦。
这是我的片段类的一部分:
public class FragmentStoreFinderMap extends Fragment implements OnMapReadyCallback {
// Google Map
private GoogleMap googleMap;
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(
R.id.map)).getMapAsync(this);
我在getMapAsync(this)中收到错误。我告诉不兼容的类型。
它说:
必需:com.google.android.gms.maps.GoogleMap
发现:无效
顺便说一句,这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
在您的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">
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
在你的片段里面实现这个
private MapView mapView;
private GoogleMap googleMap;
如果没有onCreateView,则覆盖onCreateView,就像下面的代码一样
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.your_layout, container, false);
}
在onViewCreated()内部覆盖onViewCreated()
mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this);//when you already implement OnMapReadyCallback in your fragment
当你已经在你的片段中实现OnMapReadyCallback时,将这个/代码放在这样的代码中
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
}
希望这能帮助你。
在我脑子里有几点过热之后,感谢这篇文章和https://stackoverflow.com/a/34732804/3925554,我想说目前有两种方法可以将MapFragment添加到你的应用程序中(对不起,见下文):
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
通过这种方式,您必须使用标准片段并在其中实现。实际上你是在另一个片段中插入一个片段:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);
}
另一种方式是:
<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=".ui.main.MainActivity">
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
在这种情况下,您可以使用真正的SupportMapFragment并以这种方式实现它:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getMapAsync(this);
}
而且他们无需在qazxsw poi内手动调用qazxsw poi和qazxsw poi,无论如何不推荐。
希望能帮助到你!
更新:当我发布这个时,我测试了两种方式并且它们正在工作,我发誓!我选择了第二个选项,我不知道为什么但是昨天mapView突然变成空白,没有错误也没有例外,切换到第一个选项已经修复了它。诅咒你谷歌!为什么?!
mapView.onCreate(savedInstanceState);
您的代码返回此错误,因为mapView.onResume();
不返回任何内容。如果您查看此功能的文档:
public void getMapAsync(OnMapReadyCallback callback)
设置一个回调对象,该对象将在准备好使用GoogleMap实例时触发。
注意:
必须从主线程调用此方法。回调将在主线程中执行。如果用户的设备上未安装Google Play服务,则在用户安装回调之前不会触发回调。在创建后立即销毁GoogleMap的极少数情况下,不会触发回调。回调提供的GoogleMap对象为非null。
OnMapReadyCallback是一个需要实现并通过此函数传递的接口。目前没有任何内容分配给你的onViewCreated()
变量你应该在这个代码块中设置它的值 SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flMap, mMapFragment);
fragmentTransaction.commit();
mMapFragment.getMapAsync(this);
getMapAsync()
这段代码应该在你的片段中。由于您的Fragment已经实现了此接口,因此您将其作为googleMap
传递。