【发布时间】:2019-02-18 15:57:04
【问题描述】:
您好,我在启动应用程序时不断收到此异常:
2019-02-18 16:33:14.735 2080-2080/? E/AndroidRuntime: 致命异常: main 进程:assus.oumayma.com.firebasekotlinapp,PID:2080 java.lang.RuntimeException:无法启动活动 ComponentInfo{assus.oumayma.com.firebasekotlinapp/assus.oumayma.com.firebasekotlinapp.MainActivity}:java.lang.IllegalStateException:默认 FirebaseApp 在此过程中未初始化 assus.oumayma.com .firebasekotlinapp。确保首先调用 FirebaseApp.initializeApp(Context)。 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2725) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2786) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1501) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:173) 在 android.app.ActivityThread.main(ActivityThread.java:6459) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828) 原因:java.lang.IllegalStateException:默认 FirebaseApp 未在此过程中初始化 assus.oumayma.com.firebasekotlinapp。确保首先调用 FirebaseApp.initializeApp(Context)。 在 com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.2:240) 在 com.google.firebase.auth.FirebaseAuth.getInstance(未知来源) 在 assus.oumayma.com.firebasekotlinapp.MainActivity.onCreate(MainActivity.kt:23) 在 android.app.Activity.performCreate(Activity.java:6673) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
这是代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mAuth = FirebaseAuth.getInstance()
signOut.setOnClickListener {
view: View? -> mAuth.signOut()
startActivity(Intent(this, PhoneAuthenfication::class.java))
Toast.makeText(this, "Logged out Successfully :)", Toast.LENGTH_LONG)
.show()
}
}
override fun onStart() {
super.onStart()
if (mAuth.currentUser == null) {
startActivity(Intent(this, PhoneAuthenfication::class.java))
} else {
Toast.makeText(this, "Already Signed in :)", Toast.LENGTH_LONG).show()
}
}
}
class PhoneAuthenfication : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_phone_authenfication)
mAuth = FirebaseAuth.getInstance()
veriBtn.setOnClickListener { view: View? ->
progress.visibility = View.VISIBLE
verify()
}
authBtn.setOnClickListener { view: View? ->
progress.visibility = View.VISIBLE
authenticate()
}
}
private fun verificationCallbacks() {
mCallbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(credential: PhoneAuthCredential) {
progress.visibility = View.INVISIBLE
signIn(credential)
}
override fun onVerificationFailed(p0: FirebaseException?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onCodeSent(verfication: String?, p1: PhoneAuthProvider.ForceResendingToken?) {
super.onCodeSent(verfication, p1)
verificationId = verfication.toString()
progress.visibility = View.INVISIBLE
}
}
}
private fun verify() {
verificationCallbacks()
val phnNo = phnNoTxt.text.toString()
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phnNo,
60,
TimeUnit.SECONDS,
this,
mCallbacks
)
}
private fun signIn(credential: PhoneAuthCredential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener { task: Task<AuthResult> ->
if (task.isSuccessful) {
toast("Logged in Successfully :)")
startActivity(Intent(this, MainActivity::class.java))
}
}
}
private fun authenticate() {
val verifiNo = verifiTxt.text.toString()
val credential: PhoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, verifiNo)
signIn(credential)
}
private fun toast(msg: String) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
}
build.gradle:
implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.7'
【问题讨论】:
-
请添加两个 build.gradle 文件的全部内容。
标签: android firebase firebase-authentication