Я пытаюсь вызвать addAuthStateListener, но он не работает в kotlin.а также AuthUI не работает должным образом.Каждый раз, когда я пытаюсь запустить этот код, я получаю ошибку во время выполнения.И приложение силы закрыто.Я пытался получить некоторую помощь в документации Firebase, но я не нашел ничего, что мне помогло.
import android.app.Activity
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.firebase.ui.auth.AuthUI
import com.google.firebase.auth.FirebaseAuth
class MainActivity : AppCompatActivity() {
private var mFirebaseAuth:FirebaseAuth? = null
private var mFirebaseAuthLis:FirebaseAuth.AuthStateListener?=null
private val RC_SIGN_IN = 1258
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mFirebaseAuth = FirebaseAuth.getInstance()
//val user = FirebaseAuth.getInstance().currentUser != null
mFirebaseAuthLis=FirebaseAuth.AuthStateListener { firebaseAuth ->
var user = firebaseAuth.currentUser
if (user!=null){
Toast.makeText(applicationContext,"Welcome",Toast.LENGTH_SHORT).show()
}else{
val providers = arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.PhoneBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build(),
AuthUI.IdpConfig.FacebookBuilder().build(),
AuthUI.IdpConfig.TwitterBuilder().build())
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
RC_SIGN_IN)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode==RC_SIGN_IN){
if (resultCode== Activity.RESULT_OK){
Toast.makeText(applicationContext,"Hi user",Toast.LENGTH_SHORT).show()
}else{
finish()
}
}
}
override fun onStart() {
super.onStart()
mFirebaseAuth!!.addAuthStateListener { mFirebaseAuthLis }
}
override fun onPause() {
super.onPause()
mFirebaseAuth!!.removeAuthStateListener { mFirebaseAuthLis }
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.roni.familychatting">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
build.gradle (Модуль: приложение)
применить плагин: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.roni.familychatting"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation 'com.android.support:appcompat-v7:24.2.0'
implementation 'com.firebaseui:firebase-ui-auth:4.2.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
Gradle уровня проекта
buildscript {
ext.kotlin_version = '1.2.71'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}