[英]kotlin.TypeCastException: null cannot be cast to non-null type android.support.design.widget.FloatingActionButton
[英]Android kotlin.TypeCastException: null cannot be cast to non-null type com.google.android.gms.maps.SupportMapFragment
我正在尝试在 Kotlin 中编写我当前的应用程序,但我得到 null 无法转换为非 null 类型。 我尝试了很多不同的东西,但我一直遇到同样的问题。 不知道该怎么办。 任何帮助,将不胜感激!
代码:
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
private lateinit var button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val mapFragment = supportFragmentManager
.findFragmentById(R.id.mapFragment) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap;
setUpMap();
}
fun setUpMap() {
val et = findViewById<EditText>(R.id.editText);
val et2 = findViewById<EditText>(R.id.editText2);
val lat = et.getText().toString().toDouble();
val lng = et2.getText().toString().toDouble();
//val ll = LatLng(lat, lng)
button = findViewById(R.id.button) as Button
button.setOnClickListener {
goToLocation(lat, lng, 11.0f);
}
}
fun goToLocation(lat:Double, lng:Double, zoom:Float) {
val ll = LatLng(lat, lng);
val update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
mMap.addMarker(MarkerOptions().position(ll).title("Marquette, Michigan"))
mMap.moveCamera(update);
}
XML:
<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"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:layout="@layout/activity_maps">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"/>
<!-- android:onClick="locate"/> -->
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/mapFragment"
/>
日志:
引起: kotlin.TypeCastException: null 不能转换为非空类型 com.google.android.gms.maps.SupportMapFragment at com.example.nrice.mapsproject.MapsActivity.onCreate(MapsActivity.kt:38)
尝试这个
val mapFragment = childFragmentManager.findFragmentById(R.id.map_frag) as SupportMapFragment
mapFragment.getMapAsync(this)
编译错误指向代码中的以下行:
val mapFragment = supportFragmentManager
.findFragmentById(R.id.mapFragment) as SupportMapFragment
在这里,您将可以为空的内容(即findFragmentById
的返回值)类型转换为您已定义为不可为空类型的内容,即SupportMapFragment
理想情况下,您应该阅读 Kotlin 空处理。 它是快速阅读,不应该花时间,并会在这里清除您的理解。
而且,当您回来时,您会发现修复代码的方法不止一种,例如:
(activity.supportFragmentManager.findFragmentById(fragmentId) as SupportMapFragment?)?.let {
it.getMapAsync(this)
}
快速提示:Kotlin 中可空类型和不可空类型之间的区别是?
后缀到类型。 那么,在这里, SupportMapFragment?
指定类型为 SupportMapFragment 并且可以为 null。
val mapFragment = supportFragmentManager.findFragmentById(R.id.mapFragment) as? SupportMapFragment
mapFragment?.getMapAsync(this)
作为参考,你应该阅读这个
如果您在 Fragment 中使用SupportMapFragment
,则必须在其中编写“childFragmentManager.findFragmentById(R.id.myMap)”语句
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
不在
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
在 kotlin 中修复这里
private lateinit var mMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mapFragment: SupportMapFragment? = map as SupportMapFragment?
mapFragment?.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap?) {
if (googleMap != null) {
mMap = googleMap
}
val sydney = LatLng(-34.0, 151.0)
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.