I have searched a lot before posting this question
so it seams like I can't show the map using MapsActivity - I'm using Kotlin -
so this is my code
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))
}
}
I have tried everything I know and I'm already writing the same code as the instructor but mine doesn't work and it gave me Error: null cannot be cast to non-null type com.google.android.gms.maps.supportmapfragment for this line
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
so after debugging this line returns null and I don't know why - it's built in not mine - I have even tried to remove all my adjustments for this file but the map didn't show up either
and this is the instructors code:
`
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))
}
}
`
as you can see there is no deference between the two codes
so if anyone can help I'll be great full