为什么Firebase Realtime数据库无法正常工作

时间:2019-06-25 13:12:53

标签: android firebase firebase-realtime-database kotlin

我用Kotlin语言编写了代码
我需要firebase实时数据库来添加一个带有用户UID和电子邮件的子级,因为它在用户注册时在用户列下的值。登录成功,然后将其传递到主要活动。用户详细信息在“身份验证”->“用户”中列出,但实时数据库不起作用,当我单击“后退”按钮时,它还会一次又一次打开主要活动的点击次数会返回登录页面并退出,这也许是因为它无法连接到数据库。

请帮助我进行必要的更改。 这是代码

主要活动


private var mFirebaseAnalytics: FirebaseAnalytics?=null

class MainActivity : AppCompatActivity() {

    var listofContacts = ArrayList<Contacts>()
    var adapter: ContactsAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mFirebaseAnalytics= FirebaseAnalytics.getInstance(this)
        listofContacts.add(Contacts("NAME", 123456789))

        adapter = ContactsAdapter(this, listofContacts)
        listview.adapter=adapter
    }

    inner class ContactsAdapter : BaseAdapter {

        var contactslist = ArrayList<Contacts>()
        var context: Context? = null

        constructor(context: Context, contactslist: ArrayList<Contacts>) : super() {
            this.contactslist = contactslist
            this.context = context
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val contact = contactslist[position]
            val inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val myView = inflater.inflate(R.layout.contact_view, null)
            myView.name.text = contact.name!!
            myView.num.text = contact.num.toString()!!
            myView.setOnClickListener {
                val intent= Intent(context,ContactInfo::class.java!!)
                intent.putExtra("name",contact.name!!)
                intent.putExtra("num",contact.num!!)
                context!!.startActivity(intent)
            }
            return myView
        }

        override fun getItem(position: Int): Any {
            return contactslist[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getCount(): Int {
            return contactslist.size
        }

    }
}

登录

    private var mAuth:FirebaseAuth?=null

        var database=FirebaseDatabase.getInstance()
        var myRef=database.reference


        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_login)
            mAuth = FirebaseAuth.getInstance()
        }

        fun loginevent(view: View){
           login(etun.text.toString(),pw.text.toString())
        }

        fun login(email:String,password:String){
            mAuth!!.createUserWithEmailAndPassword(email,password)
                .addOnCompleteListener(this){task ->
                    if(task.isSuccessful){

                        Toast.makeText(applicationContext,"Login Successful", Toast.LENGTH_LONG).show()
                        LoadMain()
                    }
                    else
                        Toast.makeText(applicationContext,"Login Failed", Toast.LENGTH_LONG).show()
                }

        }

        override fun onStart() {
            super.onStart()
            LoadMain()
        }

        fun LoadMain(){
            var currentuser=mAuth!!.currentUser
            if(currentuser!=null) {            
           myRef.child("Users").child(currentuser.uid).setValue(currentuser.email)
                val intent = Intent(this, MainActivity::class.java)
                intent.putExtra("email", currentuser.email)
                intent.putExtra("uid", currentuser.uid)
                startActivity(intent)
            }
        }
    }

0 个答案:

没有答案