1
votes

I`m new around here and i have a question about my little project.

I set the database reference (mDatabase) for this activity pointed to the name child and attached an addValueEventListener() to listen to the datasnapshot from the firebase database console to fetch the name value from the database, but when i run to app , it displays like the following -
error image

Thanks you all,

register activity

class RegisterScreenActivity : AppCompatActivity() {

val mAuth = FirebaseAuth.getInstance()
lateinit var mDatabase : DatabaseReference



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.register_page)

    val regBtn = findViewById<View>(R.id.regBtn)as Button

    mDatabase = FirebaseDatabase.getInstance().getReference("Names")

    regBtn.setOnClickListener { view -> register()
    }

}

private fun register(){

val emailTxt = findViewById<View>(R.id.emailTxt) as EditText
val nameTxt = findViewById<View>(R.id.nameTxt) as EditText
val passwordTxt = findViewById<View>(R.id.passwordTxt) as EditText

var email = emailTxt.text.toString()
var name = nameTxt.text.toString()
var password = passwordTxt.text.toString()

if (!name.isEmpty() && !password.isEmpty() && !email.isEmpty()){
    mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(this) { task ->
        if (task.isSuccessful){
            val user = mAuth.currentUser
            val uid = user!!.uid
            mDatabase.child(uid).child("Name").setValue(name)
            Toast.makeText(this,"xxxxx",Toast.LENGTH_LONG).show()
            startActivity(Intent(this, LoginPage::class.java))

        }else{
            Toast.makeText(this,"xxxx",Toast.LENGTH_LONG).show()
        }
    }

}else{
    Toast.makeText(this,"xxxxx!",Toast.LENGTH_LONG).show()
}

}

i hold user info and after that comes login activity

class LoginPage : AppCompatActivity() {
val mAuth = FirebaseAuth.getInstance()


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.login_page)

    val loginBtn = findViewById<View>(R.id.loginBtn) as Button
    val regTxt = findViewById<View>(R.id.regTxt)as Button

    loginBtn.setOnClickListener { view -> login()
    }

    regTxt.setOnClickListener { view -> register()
    }


}

private fun login() {
    val emailTxt = findViewById<View>(R.id.emailTxt) as EditText
    val passwordTxt = findViewById<View>(R.id.passwordTxt) as EditText

    var email = emailTxt.text.toString()
    var password = passwordTxt.text.toString()

    if (!email.isEmpty() && !password.isEmpty()) {

        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                startActivity(Intent(this, Timeline::class.java))
                Toast.makeText(this, "xxxxx", Toast.LENGTH_LONG).show()
                startActivity(Intent(this,Timeline::class.java))
            } else {
                Toast.makeText(this, "xxxx",Toast.LENGTH_LONG).show()

            }

        }

    } else {
        Toast.makeText(this, "xxxxx", Toast.LENGTH_LONG).show()
    }

}

private fun register (){


    startActivity(Intent(this,RegisterScreenActivity::class.java))
}

}

and if info is correct next activity timeline

class Timeline : AppCompatActivity() {

lateinit var mDatabase: DatabaseReference


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.time_line)

    val dspTxt = findViewById<View>(R.id.dispTxt) as TextView

    mDatabase = FirebaseDatabase.getInstance().getReference("Names")

    mDatabase.addValueEventListener(object : ValueEventListener {
        override fun onCancelled(p0: DatabaseError) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onDataChange(snapshot: DataSnapshot) {
             snapshot.child("Name").toString()
            dspTxt.text ="Welcome " + snapshot.value.toString()
        }
    })

}

}

1
Can you also provide the structure of the database? I believe that data field 'names' is somewhere further in the structure. From what I've been taught, in order to access something at the bottom or middle of the structure, you have to start from the top. - Nero
Please provide code sample in your question as text, not as a screenshot. And also, provide full details of your data structure to clarify what you want to do and what to expect. - Dennis Alund

1 Answers

3
votes

There is no attribute value on the DataSnapshot object (see documentation) Please refer to the documentation on how to read values. You should use the function getValue().

myDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String name = dataSnapshot.getValue(String.class);
    }

    @Override
    public void onCancelled(DatabaseError error) {
        // TODO...
    }
});