我正在构建一个应用程序,需要在从相机中拍摄或从图像中选择图片后打开地图。它运行一个python脚本来生成一组随机的GPS坐标,我知道它运行得很好,因为我已经让它将结果输出到屏幕上,并且做得很正确。现在,当我调用GoogleMaps类打开地图并显示生成的坐标时,它会给出一个错误。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aggiemap/com.example.aggiemap.MapsActivity}: java.lang.NullPointerException: null cannot be cast to non-null type com.google.android.gms.maps.SupportMapFragment
我是用科特林写的。这是MainActivity的代码。地图以前打开过,但一旦我添加了python脚本,它就停止了工作,每次到达该点时都会关闭应用程序。
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && requestCode == pickImage) {
imageUri = data?.data
imageView.setImageURI(imageUri)
// PYTHON HERE
val py = Python.getInstance()
val pyobj = py.getModule("main")
this.lat = pyobj.callAttr("runlat").toDouble()
this.long = pyobj.callAttr("runlong").toDouble()
/* Open the map after image has been received from user
This will be changed later to instead call the external object recognition/pathfinding
scripts and then pull up the map after those finish running
*/
val intent = Intent(this, MapsActivity::class.java)
startActivity(intent)
}
if (dynamic) {
// PYTHON HERE
val py = Python.getInstance()
val pyobj = py.getModule("main")
this.lat = pyobj.callAttr("runlat").toDouble()
this.long = pyobj.callAttr("runlong").toDouble()
dynamic = false
/* Open the map after image has been received from user
This will be changed later to instead call the external object recognition/pathfinding
scripts and then pull up the map after those finish running
*/
val intent = Intent(this, MapsActivity::class.java)
startActivity(intent)
}
}
这是MapsActivity类的代码
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
private lateinit var binding: ActivityMapsBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMapsBinding.inflate(layoutInflater)
setContentView(binding.root)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.google_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 at generated coordinate
val location = LatLng(MainActivity().lat, MainActivity().long)
val zoomLevel = (17.0).toFloat()
mMap.addMarker(MarkerOptions().position(location).title("Your Estimated Location"))
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,zoomLevel))
}
}
提前感谢您的帮助。我已经在这个问题上呆了好几天了,不明白为什么它不起作用,也不明白这个错误意味着什么。
这是您的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aggiemap/com.example.aggiemap.MapsActivity}:
java.lang.NullPointerException: null cannot be cast to non-null type com.google.android.gms.maps.SupportMapFragment
如果null cannot be cast to non-null type SupportMapFragment
表示您正试图将某些内容(使用as
(强制转换为非null类型的SupportMapFragment
,但实际强制转换的内容是null// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.google_map) as SupportMapFragment
FragmentManager
中不存在具有该ID的片段,则findFragmentById
将返回null。因此,您需要强制转换为可为null的类型(SupportMapFragment?
(并处理null,或者在调用之前确保片段确实存在
onMapReady(...)
是用null
参数调用的。
现在,当您用非null参数googleMap: GoogleMap
覆盖函数onMapReady
时,Kotlin编译器会自动生成验证代码,如果参数为null,则会抛出遇到的异常,从而强制参数为非null。
由于该参数可以为null,因此应该将其标记为可为null,这样就不会生成验证代码-googleMap: GoogleMap?
(此外,如果它为null,还需要添加处理(。
完整签名:
override fun onMapReady(googleMap: GoogleMap?) {
mMap = googleMap
// Add a marker at generated coordinate
val location = LatLng(MainActivity().lat, MainActivity().long)
val zoomLevel = (17.0).toFloat()
mMap.addMarker(MarkerOptions().position(location).title("Your Estimated Location"))
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,zoomLevel))
}
干杯