I'm using Kotlin with Android Studio 4.2.2
I have a form that releases informations to Firebase for a map application. It worked well but, it's been some days now that I have an error. When the user click on the button to confirm the form, the app crash and the next error appear :
The data is not sended to fire base. Also, the app is no longer able to retrieve other data from Firebase in the other activity.
the error is from this line ->
val addresses = gc.getFromLocationName(ville, 2)
Here is the code :
class ValiderFormulaireMission : AppCompatActivity() {
private lateinit var profilId : String
private lateinit var firebaseUser: FirebaseUser
private var storagePostPicRef : StorageReference? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_valider_formulaire_mission)
storagePostPicRef = FirebaseStorage.getInstance().reference.child("Post Picture")
firebaseUser = FirebaseAuth.getInstance().currentUser!!
val pref = this.getSharedPreferences("PREFS", Context.MODE_PRIVATE)
if (pref != null)
{
this.profilId = pref.getString("profilId", "none")!!
}
userInfo()
btnValider.setOnClickListener {
if (
formCodePostal.text.toString() == "") {
textErreur.visibility = View.VISIBLE
textErreur.text = "Oups.. Indiquez une adresse valide !"
}
else {
textErreur.visibility = View.GONE
val ville: String = formCodePostal.text.toString()
val gc = Geocoder(this, Locale.getDefault())
// ERROR IS HERE -> val addresses = gc.getFromLocationName(ville, 2)
val address = addresses.get(0)
val usersRef = FirebaseDatabase.getInstance().reference.child("Mission")
val postId = usersRef.push().key
val postMap = HashMap<String, Any>()
postMap["postid"] = postId!!
postMap["nommission"] = nomMission.text.toString().toLowerCase()
postMap["prixmission"] = textPrix.text.toString().toLowerCase()
postMap["firstname"] = nomUtilisateur.text.toString().toLowerCase()
postMap["adresse"] = formCodePostal.text.toString().toLowerCase()
postMap["latitude"] = address.latitude
postMap["longitude"] = address.longitude
postMap["ville"] = address.locality
// Récupère directement le user depuis firebase
postMap["publisher"] = FirebaseAuth.getInstance().currentUser!!.uid
usersRef.child(postId).updateChildren(postMap)
Toast.makeText(this, "La mission a été créé.", Toast.LENGTH_LONG)
val intent = Intent(this, MenuPage::class.java)
startActivity(intent)
finish()
}
}
btnAnnuler.setOnClickListener {
val intent = Intent(this, MenuPage::class.java)
startActivity(intent)
}
val recdData = intent.extras
val myValMission = recdData!!.getString("name")
val myValPrix = recdData!!.getString("prix")
nomMission.text = myValMission
textPrix.text = myValPrix
}
private fun userInfo() {
val usersRef = FirebaseDatabase.getInstance().getReference().child("User").child(profilId)
usersRef.addValueEventListener(object : ValueEventListener
{
override fun onDataChange(p0: DataSnapshot) {
if (p0.exists()) {
val user = p0.getValue<User>(User::class.java)
// Prénom utilisateur
nomUtilisateur.text = user!!.getFirstName()
}
}
override fun onCancelled(p0: DatabaseError) {
}
}
)
}
override fun onStop() {
super.onStop()
val pref = this.getSharedPreferences("PREFS", Context.MODE_PRIVATE)?.edit()
pref?.putString("profilId", firebaseUser.uid)
pref?.apply()
}
override fun onPause() {
super.onPause()
val pref = this.getSharedPreferences("PREFS", Context.MODE_PRIVATE)?.edit()
pref?.putString("profilId", firebaseUser.uid)
pref?.apply()
}
override fun onDestroy() {
super.onDestroy()
val pref = this.getSharedPreferences("PREFS", Context.MODE_PRIVATE)?.edit()
pref?.putString("profilId", firebaseUser.uid)
pref?.apply()
}
}
Here is the imports :
import android.content.Context
import android.content.Intent
import android.location.Geocoder
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.givenaskv1.MenuPage
import com.example.givenaskv1.R
import com.example.givenaskv1.models.User
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import com.google.firebase.storage.FirebaseStorage
import com.google.firebase.storage.StorageReference
import kotlinx.android.synthetic.main.activity_valider_formulaire_mission.*
import java.util.*
import kotlin.collections.HashMap
And a screen of the form :
Can you help me please ?
Thanks !