IllegalStateExpection:FirebaseApp名称[DEFAULT]是否已存在?

时间:2018-02-22 15:00:40

标签: android firebase firebase-realtime-database kotlin firebase-authentication

基本上,我正在尝试根据用户角色将用户重定向到单独的活动,我的系统正试图通过他们输入的凭据来判断。

但突然我的应用程序崩溃了,发出以下错误: -

  

E / AndroidRuntime:致命异常:主要                     处理:com.devanshisukhija.sicsrattendance,PID:13964                     java.lang.RuntimeException:无法创建应用程序com.devanshisukhija.sicsrattendance.Controller.App:java.lang.IllegalStateException:FirebaseApp名称[DEFAULT]已经存在!                         在android.app.ActivityThread.handleBindApplication(ActivityThread.java:5743)                         在android.app.ActivityThread.-wrap1(未知来源:0)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1656)                         在android.os.Handler.dispatchMessage(Handler.java:106)                         在android.os.Looper.loop(Looper.java:164)

<>我不知道我哪里出错了?在这里,看看我的登录活动:

package com.devanshisukhija.sicsrattendance.Controller

import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.Toast
import com.devanshisukhija.sicsrattendance.R
import com.google.firebase.FirebaseApp
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import kotlinx.android.synthetic.main.activity_login.*


/**
 *  Comments template :-
 *  - //[START ]
 *  - //[End ]
 *
 */

class LoginActivity : AppCompatActivity() {

    val TAG = "LoginActivity"
    //private var mDatabaseReference: DatabaseReference? = null
    //private var mDatabase: FirebaseDatabase? = null

    // Firebase refferences for Authentication.
    private var mAuth: FirebaseAuth? = null
    private var mUser : FirebaseUser? = null
    private var mDatabase : FirebaseDatabase? = null
    private var mDatabaseReference : DatabaseReference? = null
    private var mAuthListener : FirebaseAuth.AuthStateListener? = null

    //global variables
    private var emailString : String? = null
    private var passwordString : String? = null

    lateinit var mValueEventListener : ValueEventListener
    lateinit var mDataSnapshot: DataSnapshot

    // ActivityState : ONCREATE.
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        if (!FirebaseApp.getApps(this).isEmpty()) {
            FirebaseDatabase.getInstance().setPersistenceEnabled(true)
        }

        // initializing firebase Auth and database Reference.
        mAuth = FirebaseAuth.getInstance()
        mDatabase = DatabaseHelper.getInstance()
        mDatabaseReference = FirebaseDatabase.getInstance().reference
        // getting the currently logined user.
        mUser = mAuth?.currentUser


    }
    //ActivityState : ONSTART.
    override fun onStart() {
        super.onStart()

        //[START auth_state_listener]
        mAuth?.addAuthStateListener(mAuthListener!!)
    }
    //ActivityState : ONPAUSE.
    override fun onPause() {
        super.onPause()


    }

    // function for when the login button is clicked.
    // TODO : Login Activity : Function# 1.
     fun loginBtnClicked(view : View) {

         emailString = loginEmailTxt.text.toString()
         passwordString = loginPasswordtxt.text.toString()

         if(!emailString.isNullOrEmpty() && !passwordString.isNullOrEmpty()) {

             // Checking if the login cridentials are correct. and then changing the Auth State to logged in.
             //TODO : Login Activity : Function# 3.
             mAuth!!.signInWithEmailAndPassword(emailString!!, passwordString!!).addOnCompleteListener(this) { task ->

                 if(task.isSuccessful) {
                        val uid = mUser!!.uid
                     // function call for checking the user.
                        var ref = mDatabaseReference?.child("Users")?.child("Faculty")

                    // { Here below, trying retrive a key by its value.
                     ref?.addValueEventListener(mValueEventListener)
                     mValueEventListener.onDataChange(mDataSnapshot)
                     mDataSnapshot.children.forEach {
                         if (it.value == uid) {
                             Log.d("", it.key)
                         }
                         else {
                             Log.d("WOWOWO", " nononononono")
                         }
                     }

                 } else {

                     Log.e(TAG, "signInWithEmail:failure", task.exception)
                     Toast.makeText(this@LoginActivity, "Authentication failed. Make sure email and password are correct",
                             Toast.LENGTH_SHORT).show()
                 }
             } // <-----------------End of SignInwithEmailandPassword func.-------------------------->

         } else {
             Toast.makeText(this, "Email or Password can not be empty.", Toast.LENGTH_LONG).show()
         } // <----------------End of isOrnot email and password Empty condition.------------------------->
    } //<----------------------End of Login Button clicked.------------>


    // TODO : Login Activity : Function#2
     fun getHelpImgClicked(view : View) {
        val builder = AlertDialog.Builder(this)
        val dialogView = layoutInflater.inflate(R.layout.get_help_dialog, null)
        builder.setView(dialogView)
                .setNegativeButton("Close" ){ _, _ -> }.show()

        }

    //TODO : Login Activity : Function#3
    fun checkUsertype() {


    }




}

另外,我的应用程序类:

package com.devanshisukhija.sicsrattendance.Controller

import android.app.Application
import com.devanshisukhija.sicsrattendance.BuildConfig
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions

class App : Application() {

    override fun onCreate() {
        super.onCreate()

        val options = FirebaseOptions.Builder()
                .setApplicationId(BuildConfig.APPLICATION_ID)
                .setApiKey("AIzaSyBUFy5QFOK1YDqJu4uatksBGX9nR2B4obM")
                .setDatabaseUrl("https://sicsr-d4771.firebaseio.com")
                .build()
            FirebaseApp.initializeApp(this, options)
    }
}

0 个答案:

没有答案