Android Studio 应用不断崩溃并显示屏幕投射错误
Android studio app keeps on crashing and showing screen cast error
Caused by: java.lang.ClassCastException:
androidx.constraintlayout.widget.ConstraintLayout cannot be cast to
android.widget.EditTextat
com.example.login.MainActivity.onCreate(MainActivity.kt:18)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这是我的 kotlin 代码,其中出现错误:
val Name = findViewById<EditText>(R.id.etName)
val Password = findViewById<EditText>(R.id.etpassword)
val Login = findViewById<Button>(R.id.btnlogin)
val Info = findViewById<TextView>(R.id.tvinfo)
?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
看起来你有
android:id="@+id/etName"
在您的根 ConstraintLayout
中。在代码中你有
val Name = findViewById<EditText>(R.id.etName)
它通过 ID etName
找到一个视图并尝试将其转换为 EditText
。 findViewById()
中的算法只是一个简单的树搜索,在第一次匹配时停止,这是您的根布局。如果您有另一个具有相同 ID 的视图(例如您的 EditText
),则搜索永远不会达到那么深。砰,ClassCastException
.
要修复它,请从根布局中删除 id,或将其更改为布局文件中唯一的内容,并确保布局中确实有一个 ID 为 etName
的 EditText
。
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditTextat com.example.login.MainActivity.onCreate(MainActivity.kt:18) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这是我的 kotlin 代码,其中出现错误:
val Name = findViewById<EditText>(R.id.etName)
val Password = findViewById<EditText>(R.id.etpassword)
val Login = findViewById<Button>(R.id.btnlogin)
val Info = findViewById<TextView>(R.id.tvinfo)
?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
看起来你有
android:id="@+id/etName"
在您的根 ConstraintLayout
中。在代码中你有
val Name = findViewById<EditText>(R.id.etName)
它通过 ID etName
找到一个视图并尝试将其转换为 EditText
。 findViewById()
中的算法只是一个简单的树搜索,在第一次匹配时停止,这是您的根布局。如果您有另一个具有相同 ID 的视图(例如您的 EditText
),则搜索永远不会达到那么深。砰,ClassCastException
.
要修复它,请从根布局中删除 id,或将其更改为布局文件中唯一的内容,并确保布局中确实有一个 ID 为 etName
的 EditText
。