在发布这个问题之前,我已经搜索了很多
所以它接缝就像我无法使用MapsActivity显示地图 - 我正在使用Kotlin -
所以这是我的代码
package com.example.digit.findmyphone
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
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
import com.google.firebase.database.*
import java.util.HashMap
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
var myRef:DatabaseReference?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val bundle = intent.extras
val phoneNumber = bundle.getString("phoneNumber")
myRef = FirebaseDatabase.getInstance().reference
myRef!!.child("Users").child(phoneNumber).child("location")
.addValueEventListener(object: ValueEventListener{
override fun onCancelled(p0: DatabaseError?) {
}
override fun onDataChange(dataSnapShot: DataSnapshot?) {
try {
val tableData = dataSnapShot!!.value as HashMap<String, Any>
val latitude = tableData["latitude"].toString()
val longitude = tableData["longitude"].toString()
MapsActivity.sydney = LatLng(latitude.toDouble(),longitude.toDouble())
MapsActivity.lastSeen = tableData["last seen"].toString()
loadMap()
}catch (e:Exception){
Toast.makeText(applicationContext,"Error: "+e.message,Toast.LENGTH_LONG).show()
return
}
}
}
)
}
fun loadMap(){
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.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.
*/
companion object {
var sydney = LatLng(-34.0, 151.0)
var lastSeen = "not defined"
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in Sydney and move the camera
mMap.addMarker(MarkerOptions().position(sydney).title(lastSeen))
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,15f))
}
}
我已经尝试了我所知道的所有内容,并且我已经编写了与教师相同的代码但是我的工作没有用,它给了我错误:null无法转换为非null类型的com.google.android.gms。此行的maps.supportmapfragment
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
所以调试后这行返回null,我不知道为什么 - 它是内置的不是我的 - 我甚至试图删除我对该文件的所有调整但是地图没有显示
这是教官代码:
`
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
var dDatabaseRef:DatabaseReference?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val bundle:Bundle=intent.extras
val phoneNumber =bundle.getString("phoneNumber")
dDatabaseRef=FirebaseDatabase.getInstance().reference
dDatabaseRef!!.child("Users").child(phoneNumber)
.child("location").addValueEventListener(object:ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot?) {
try{
val td= dataSnapshot!!.value as HashMap<String,Any>
val lat =td["lat"].toString()
val log =td["log"].toString()
MapsActivity.sydney= LatLng(lat.toDouble(),log.toDouble())
MapsActivity.lastOnline= td["lastOnline"].toString()
loadMap()
}catch (ex:Exception){}
}
override fun onCancelled(p0: DatabaseError?) {
}
})
}
fun loadMap(){
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.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.
*/
companion object{
var sydney = LatLng(-34.0, 151.0)
var lastOnline="not defined"
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in Sydney and move the camera
mMap.addMarker(MarkerOptions().position(sydney).title(lastOnline))
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,15f))
}
}
`
正如你所看到的,两个代码之间没有任何差别
所以,如果有人可以提供帮助我将会非常充实