android - 使用地图片段(kotlin)时,活动在充气时崩溃
问题描述
我正在制作一个具有许多地图交互的应用程序,其中一个是在 Activity 中看到一个小地图,用户可以在其中选择地图中的一个点并向我们的 API 发送请求。
我的问题是,在我制作片段以加载谷歌地图并将其添加到应该显示它的活动后,活动本身在登陆时开始崩溃。调试应用程序,我看到崩溃发生在 Activity 的“SetContentView”上。
这是活动的 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FaleConoscoActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:id="@+id/ll_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="25dp"
android:paddingTop="70dp"
android:paddingRight="25dp"
android:paddingBottom="40dp"
app:layout_anchor="@+id/ll_main_layout"
app:layout_anchorGravity="center">
//Some EditTexts and Buttons
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<fragment
android:id="@+id/fragment"
android:name="com.example.dumper2.MapFragment"
tools:layout="@layout/fragment_map"
android:layout_width="match_parent"
android:layout_height="294dp" />
</FrameLayout>
</LinearLayout>
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
活动的 Kotlin 代码:
class CadastroDePontoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cadastro_de_ponto)
}
//Some other interactions
}
以下是片段的 XML 和 Kotlin:
<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=".MapFragment">
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/mapFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
科特林:
class MapFragment : Fragment(), OnMapReadyCallback {
private var param1: String? = null
private var param2: String? = null
private var listener: OnFragmentInteractionListener? = null
private lateinit var mMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.fragment_map, container,
false)
val mapFragment = childFragmentManager.findFragmentById(R.id.mapFrag)as SupportMapFragment
mapFragment.getMapAsync(this)
return rootView
}
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
listener = context
} else {
throw RuntimeException(context.toString() + " must implement
OnFragmentInteractionListener")
}
}
override fun onDetach() {
super.onDetach()
listener = null
}
interface OnFragmentInteractionListener {
// TODO: Update argument type and name
fun onFragmentInteraction(uri: Uri)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment MapFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
MapFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
解决方案
如果没有崩溃日志,它只是猜测,但我看不到您的活动扩展 OnFragmentInteractionListener,因此在 onAttach 中它会抛出 RuntimeException。
推荐阅读
- html - 内容不够溢出时如何粘底?
- excel - 在 Excel 中查找今天和特定日期之间的月数
- python - 在 For 循环中更新字典中的变量
- javascript - webcomponents 中允许的页面上的非唯一 ID
- swift - 如何通过 UIButton 从 DetailViewController 将数据发送到 TableViewController?
- android - 打开蓝牙启动 MediaBrowserServiceCompat
- azure - 如何在 Azure 容器实例中设置 cloud_RoleName?
- d3.js - d3将文本悬停到剪贴路径
- python - 如何从用户代理标题中删除方括号?
- google-play - Google play 无法识别电子邮件地址(内部测试)