我正在尝试在片段中使用SupportMapFragment。 在第一次启动时,地图显示没有任何错误。
但是当我对另一个片段执行一个事务,然后返回到地图后,我有一个错误的膨胀片段。但是当我创建包含地图的片段的新实例时,我没有任何错误。
这是我的布局:
<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"
android:animateLayoutChanges="true"
tools:context=".ui.HomeMapFragment">
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/footer"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:name="com.google.android.gms.maps.SupportMapFragment" />
<FrameLayout
android:id="@+id/footer"
android:layout_height="110dp"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@color/colorPrimary"/>
</RelativeLayout>
我在onCreateView中实现了我的SupportMapFragment,如下所示:
//Instantiate the map fragment
mHolder.supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
我在onActivityCreated中调用onMapReady:
if (mHolder.supportMapFragment != null)
mHolder.supportMapFragment.getMapAsync(this);
错误日志:
FATAL EXCEPTION:
main Process: xxx.xxxx.xxxx, PID: 4441
android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at xxx.xxxx.xxxx.ui.HomeMapFragment.onCreateView(HomeMapFragment.java:234)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:971)
at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1670)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:586)
at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:189)
at com.tapptic.collecto.ui.HomeActivity.onBackPressed(HomeActivity.java:71)
我知道在片段布局中对片段进行硬编码并不是一件好事,但我已经尝试使用frameLayout执行此操作但我得到更多错误
答案 0 :(得分:4)
如果要在片段中使用map,则需要使用MapView而不是SupportMapFragment How to use MapView in android using google map V2?
答案 1 :(得分:3)
2017年作品(Kotlin):
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val mapFragment = childFragmentManager.findFragmentById((R.id.map)) as SupportMapFragment
mapFragment.getMapAsync(this)
}