简体   繁体   English

Kotlin 多平台应用程序在启动时崩溃

[英]Kotlin Multiplatform app crashes on start

When I run my app, it is supposed to show a screen for a user to create an account or sign in. I had the code working as a regular kotlin app and decided to make a multiplatform app instead so I remade the project as multiplatform.当我运行我的应用程序时,它应该显示一个屏幕供用户创建帐户或登录。我让代码作为一个普通的 kotlin 应用程序工作,并决定制作一个多平台应用程序,所以我将项目重新制作为多平台。 KMM does not support this code: KMM 不支持此代码:

import kotlinx.android.synthetic.main.activity_sign_in.*

so I had to change to ViewBinding.所以我不得不改用 ViewBinding。 Here is the code I used previously and what I changed it to:这是我之前使用的代码以及我将其更改为:

Before:前:

import android.content.Intent
import android.os.Bundle
import android.widget.EditText
import com.cj.globekotlin.Extensions.toast
import com.cj.globekotlin.FirebaseUtils.firebaseAuth
import kotlinx.android.synthetic.main.activity_sign_in.*

class SignInActivity : AppCompatActivity() {
    lateinit var signInEmail: String
    lateinit var signInPassword: String
    lateinit var signInInputsArray: Array<EditText>

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

        signInInputsArray = arrayOf(etSignInEmail, etSignInPassword)
        btnCreateAccount2.setOnClickListener {
            startActivity(Intent(this, CreateAccountActivity::class.java))
            finish()
        }

        btnSignIn.setOnClickListener {
            signInUser()
        }
    }

    private fun notEmpty(): Boolean = signInEmail.isNotEmpty() && signInPassword.isNotEmpty()

    private fun signInUser() {
        signInEmail = etSignInEmail.text.toString().trim()
        signInPassword = etSignInPassword.text.toString().trim()

        if (notEmpty()) {
            firebaseAuth.signInWithEmailAndPassword(signInEmail, signInPassword)
                .addOnCompleteListener { signIn ->
                    if (signIn.isSuccessful) {
                        startActivity(Intent(this, HomeActivity::class.java))
                        toast("signed in successfully")
                        finish()
                    } else {
                        toast("sign in failed")
                    }
                }
        } else {
            signInInputsArray.forEach { input ->
                if (input.text.toString().trim().isEmpty()) {
                    input.error = "${input.hint} is required"
                }
            }
        }
    }
}

Current:当前的:

import android.content.Intent
import android.os.Bundle
import android.widget.EditText
import com.cj.globemultiplatform.android.Extensions.toast
import com.cj.globemultiplatform.android.FirebaseUtils.firebaseAuth
import com.cj.globemultiplatform.android.databinding.ActivitySignInBinding

class SignInActivity : AppCompatActivity() {
    lateinit var signInEmail: String
    lateinit var signInPassword: String
    lateinit var signInInputsArray: Array<EditText>

    private lateinit var binding: ActivitySignInBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySignInBinding.inflate(layoutInflater)
        setContentView(R.layout.activity_sign_in)

        signInInputsArray = arrayOf(binding.etSignInEmail, binding.etSignInPassword)
        binding.btnCreateAccount2.setOnClickListener {
            startActivity(Intent(this, CreateAccountActivity::class.java))
            finish()
        }

        binding.btnSignIn.setOnClickListener {
            signInUser()
        }
    }

    private fun notEmpty(): Boolean = signInEmail.isNotEmpty() && signInPassword.isNotEmpty()

    private fun signInUser() {
        signInEmail = binding.etSignInEmail.text.toString().trim()
        signInPassword = binding.etSignInPassword.text.toString().trim()

        if (notEmpty()) {
            firebaseAuth.signInWithEmailAndPassword(signInEmail, signInPassword)
                .addOnCompleteListener { signIn ->
                    if (signIn.isSuccessful) {
                        startActivity(Intent(this, HomeActivity::class.java))
                        toast("signed in successfully")
                        finish()
                    } else {
                        toast("sign in failed")
                    }
                }
        } else {
            signInInputsArray.forEach { input ->
                if (input.text.toString().trim().isEmpty()) {
                    input.error = "${input.hint} is required"
                }
            }
        }
    }
}

Initially, I thought that this would work but now whenever I open the app, it crashes.最初,我认为这会起作用,但现在每当我打开应用程序时,它就会崩溃。 I think this change is what's causing the crashes.我认为这种变化是导致崩溃的原因。 Is that possible?那可能吗? If so, how can I fix this?如果是这样,我该如何解决这个问题?

There are a couple of problems with your android code:你的安卓代码有几个问题:

  1. There is no connection between your inflated binding and the content you are setting, you should use您的膨胀绑定和您设置的内容之间没有联系,您应该使用
binding = ActivitySignInBinding.inflate(layoutInflater)
setContentView(binding.root) 
  1. Your lateinit var iables for the inputs are used in the signInUser() method, but it's not initialised.lateinit var用于输入iables都在signInUser()方法中使用,但它不会被初始化。 I'd suggest removing that and using binding.signInEmail and the other views.我建议删除它并使用binding.signInEmail和其他视图。

Also, if you're trying to share code between Android & iOS, you should be aware that you'll need to abstract away any platform specific implementation.此外,如果您尝试在 Android 和 iOS 之间共享代码,您应该意识到您需要抽象掉任何特定于平台的实现。 For ex: all packages that are android specific will not work on iOS at this moment.例如:目前所有特定于 android 的软件包都无法在 iOS 上运行。 This specific code is pretty Android platform heavy, thus I wouldn't even try to share this in a KMM app, only business logic, perhaps up to a ViewModel layer.这个特定的代码是相当重的 Android 平台,因此我什至不会尝试在 KMM 应用程序中共享它,只有业务逻辑,也许直到 ViewModel 层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kotlin:片段崩溃应用程序上的 setOnClickListener - Kotlin: setOnClickListener on Fragment Crashes app 启动App时AdMob崩溃 - AdMob crashes when start App 如何从 Android Studio 加速 Kotlin Multiplatform 的 iOS 应用构建 - How to speed up iOS app builds of Kotlin Multiplatform from Android Studio Kotlin Multiplatform Mobile 可以与 Jetpack Compose 一起使用吗? - Could Kotlin Multiplatform Mobile work with Jetpack Compose? Kotlin 多平台项目中的语法高亮显示失败 - Syntax highlighting fails in Kotlin Multiplatform project Kotlin 当我尝试通过 BottomNavigation 导航时应用程序崩溃 - Kotlin App crashes when I try to navigate through BottomNavigation kotlin 应用程序在转到另一个活动时崩溃 - kotlin app crashes when going one to another activity Kotlin 多平台错误:指令与文件位置不匹配 - Kotlin Multiplatform error: directive doesn't match file location 如何使 Kotlin Multiplatform 与 Android Studio 的项目视图一起使用? - How to make Kotlin Multiplatform work with Android Studio's project view? 将 Ktor 添加到 Kotlin Multiplatform Mobile 导致未解决的参考:HttpClient - Adding Ktor To Kotlin Multiplatform Mobile results in Unresolved reference: HttpClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM