【发布时间】:2020-11-26 07:08:12
【问题描述】:
我正在尝试使用片段在 android 中创建实现 Google-Map:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_map_test, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//the line below get's error
val mapFragment = view?.findViewById<View>(R.id.sp_map_test) as SupportMapFragment
mapFragment.getMapAsync(this)
}
我的 XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MapTestFragment">
<fragment
android:id="@+id/sp_map_test"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
但它找不到 SupportMap 片段。我该如何解决这个问题?
我也试过这个 - val mapFragment = parentFragmentManager.findFragmentById(R.id.sp_map_test) as SupportMapFragment
null 不能转换为非 null 类型 com.google.android.gms.maps.SupportMapFragment
【问题讨论】:
标签: android google-maps kotlin android-fragments