[英]Android Google Map Fragment in another Fragment
我似乎无法在片段中添加谷歌地图。 这是我的代码
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
错误android.view.InflateException: Binary XML file line #8: Error inflating class fragment
。
我已经尝试过在片段中使用MapView
,就像这篇文章或这篇 文章一样。 但问题是,现在不建议使用mapView.getMap()
。 虽然该程序没有出错,但显示的地图却是空白的(只在左下角显示Google徽标)。
任何建议表示赞赏。
是的,现在不建议使用“ mapView.getMap()” 。 使用“ getMapAsync”当准备好使用地图时将触发的对象。
对于错误' 错误膨胀类片段。 '请检查项目的构建目标和最低目标SDK版本。 另外,不要忘记包含您的地图权限。
我找到了与您的问题有关的相关SO凭单 。 其他OP提供了一些解决方案,它可能会解决您的问题。
您还可以使用官方Google文档for Maps Android API,此文档可以快速了解如何创建Google地图到Android应用: https : //developers.google.com/maps/documentation/android-api/start
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.