【问题标题】:Android kotlin.TypeCastException: null cannot be cast to non-null type com.google.android.gms.maps.SupportMapFragmentAndroid kotlin.TypeCastException: null 不能转换为非 null 类型 com.google.android.gms.maps.SupportMapFragment
【发布时间】:2018-11-29 22:53:42
【问题描述】:

我正在尝试在 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 不能转换为非 null 类型 com.google.android.gms.maps.SupportMapFragment 在 com.example.nrice.mapsproject.MapsActivity.onCreate(MapsActivity.kt:38)

【问题讨论】:

标签: android google-maps kotlin supportmapfragment


【解决方案1】:

试试这个

val mapFragment = childFragmentManager.findFragmentById(R.id.map_frag) as SupportMapFragment

mapFragment.getMapAsync(this)

【讨论】:

  • 这对我有用,我在 Fragment 中使用 MapFragment,所以 childFragmentManager 做得很好,而不是 supportFragmentManager,谢谢!
  • 这是您正在寻找的答案:stackoverflow.com/a/38405063/2445763;不用作?如果您不期望 null 值
【解决方案2】:

编译错误指向您代码中的以下行:

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。

【讨论】:

    【解决方案3】:
    val mapFragment = supportFragmentManager.findFragmentById(R.id.mapFragment) as? SupportMapFragment
    mapFragment?.getMapAsync(this)
    

    作为参考,你应该阅读this

    【讨论】:

      【解决方案4】:

      如果您在片段中使用SupportMapFragment 您必须在

      中编写“childFragmentManager.findFragmentById(R.id.myMap)”语句
      override fun onViewCreated(view: View, savedInstanceState: Bundle?)
      

      不在

      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
      

      【讨论】:

      • 虽然这不是与问题相关的答案。但是像我这样的人得到了我正在使用片段的确切解决方案。谢谢。这就是我投票给你的原因。
      【解决方案5】:

      在 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))
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-09
        相关资源
        最近更新 更多