강의

멘토링

커뮤니티

Inflearn Community Q&A

tkhpsch1166's profile image
tkhpsch1166

asked

Create 1:1 Chat (Android + Kotlin + Firebase)

DB Input

빨간색 글씨때문에 막혀요. 그리고 Cloud Firestore 초기화 코드가 달라요.

Written on

·

898

0

val user =User(uid, username.text.toString())
에서 username부분이 빨간색이 떠요.
val db = FirebaseFirestore.getInstance.collection("users")
에서 FirebaseFirestore가 빨간색이 떠요.
addOnSuccessListener{
Log.d(TAG, "데이터베이스 성공")
}
addOnFailureListener{
Log.d(TAG, "데이터베이스 실패")
}
에서
addOnSuccessListener와 addOnFailureListener가 빨간색이 떠요.
multiDexEnabled true와 "com.android.support:multidex:1.0.3"은 어디서 나온건가요?
어디서 복사하기 붙여넣기 했나요? 파이어스토어 데이터베이스에서 검색해봐도 똑같은
코드가 없습니다. 어떻게 했는지 모르겠어요.
package com.example.chatting_video

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_main.*
import android.util.Log as Log
import kotlinx.android.synthetic.main.activity_main.login_button_main as login_button_main


class MainActivity : AppCompatActivity() {

private lateinit var auth: FirebaseAuth// ...

private val TAG: String = MainActivity::class.java.simpleName


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


auth = FirebaseAuth.getInstance()

join_button.setOnClickListener {

val email = email_area.text.toString()
val password = password_area.text.toString()
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information

Log.d(TAG, "성공")

val uid = FirebaseAuth.getInstance().uid



val user =User(uid, username.text.toString())

// 여기에서 데이터베이스에 넣음
val db = FirebaseFirestore.getInstance.collection("users")
db.document(uid)
.set(user)
addOnSuccessListener{
Log.d(TAG, "데이터베이스 성공")
}
addOnFailureListener{
Log.d(TAG, "데이터베이스 실패")
}



val intent = Intent(this, ChatListActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)


} else {

Log.d(TAG, "실패", task.exception)


}
}
}




login_button_main.setOnClickListener {

val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}



}


}


plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.example.chatting_video"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:19.3.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation platform('com.google.firebase:firebase-bom:27.1.0')
implementation 'com.google.firebase:firebase-analytics'
implementation platform('com.google.firebase:firebase-bom:26.8.0')


}
kotlinandroidfirebase

Answer 2

0

bokchi님의 프로필 이미지
bokchi
Instructor

multiDex 관련 설명입니다.

https://mixup.tistory.com/72

0

bokchi님의 프로필 이미지
bokchi
Instructor

아래 코드를 참고해보시겠어요??

// Create a new user with a first and last name
val user = hashMapOf(
       
"first" to "Ada",
       
"last" to "Lovelace",
       
"born" to 1815
)

// Add a new document with a generated ID
db
.collection("users")
   
.add(user)
   
.addOnSuccessListener { documentReference ->
       
Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}")
   
}
   
.addOnFailureListener { e ->
       
Log.w(TAG, "Error adding document", e)
   
}


https://firebase.google.com/docs/firestore/quickstart

tkhpsch1166's profile image
tkhpsch1166

asked

Ask a question