【发布时间】:2020-06-06 06:34:04
【问题描述】:
我对此很陌生,试图在 Android Studio 中将两个项目组合在一起, 我收到以下代码的错误。第 52 行,“override fun onCreateView(”给我一个错误不会覆盖任何内容。任何人都可以修复此代码吗?在此站点上的其他地方搜索有关“Kotlin '?' 表示该值在 Kotlin 中可以为 null。”但我正在努力修复它。谢谢你xx
package com.example.version4.ui.map
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.example.version4.R
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
class MapsFragment : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.text_map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in Sydney and move the camera
val OrchidsSpa = LatLng(52.316296, -2.261900)
mMap.addMarker(MarkerOptions().position(OrchidsSpa).title("Marker in Bryony's @ Orchids Spa"))
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(OrchidsSpa,12F))
}
private lateinit var mapsViewModel: MapsViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
mapsViewModel =
ViewModelProviders.of(this).get(MapsViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_maps, container, false)
val textView: TextView = root.findViewById(R.id.text_map)
mapsViewModel.text.observe(this, Observer {
textView.text = it
})
return root
}
}
【问题讨论】:
标签: class android-fragments android-widget oncreate