我似乎无法在片段中添加谷歌地图。这是我的代码
activity_main.xml中
<RelativeLayout 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.example.test.MainActivity">
<fragment
android:id="@+id/fragment1"
android:name="com.example.test.MyMapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
fragment_mymap.xml
<RelativeLayout 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=".MainActivity" >
<fragment
android:id="@+id/google_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
MyMapFragment.java
public class MyMapFragment extends Fragment implements OnMapReadyCallback
{
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mymap, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.google_map);
mapFragment.getMapAsync(this);
// Inflate the layout for this fragment
return v;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
}
然而,我收到了android.view.InflateException: Binary XML file line #8: Error inflating class fragment
的错误。
我尝试在this post或this one中使用片段中的MapView
。但问题是,mapView.getMap()
现已弃用。虽然该程序没有出错,但显示的是地图,但它是空白的(只在左下角显示Google徽标)。
任何建议都表示赞赏。
答案 0 :(得分:0)
是的,&#39; mapView.getMap()&#39; 现已弃用。使用'getMapAsync'当地图准备好使用时将触发的对象。
错误&#39; 错误导致类片段。&#39;请检查项目构建目标和最小目标sdk版本。另外,不要忘记包含您的地图权限。
我发现此问题与您的问题有关SO ticket。其他OP提供了一些解决方案,它可能会解决您的问题。
您还可以使用官方Google文档for Maps Android API,此文档可以快速了解如何创建Google地图到Android应用: https://developers.google.com/maps/documentation/android-api/start