英文:
findViewById is giving me an ulcer
问题
我正在尝试创建一个登录页面,但我收到的错误消息是参数 "ID" 没有传递任何值,不知道是什么意思。
以下是 LoginActivity.kt 的代码部分:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val username = findViewById<EditText>(R.id.username)
val password = findViewById<EditText>(R.id.password)
val loginButton = findViewById<Button>(R.id.login_button)
loginButton.setOnClickListener {
val username = username.text.toString()
val password = password.text.toString()
if (username == "lukejjones" && password == "test123") {
Toast.makeText(this, "Access Granted", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Access Denied!", Toast.LENGTH_SHORT).show()
}
}
}
以下是与此相关的 activity_login.xml 代码:
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="textPersonName"
android:padding="20dp"
android:hint="@string/Username" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:padding="20dp"
android:hint="@string/Password" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@color/black"
android:text="Login"
android:textColor="@color/white"
android:onClick="handleLoginClick" />
希望这可以帮助您解决问题。
英文:
I'm trying to make a login page but the error message that i get is that there's no value passed for parameter "ID" what ever that means.
Here's the code for LoginActivity.kt:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val username = findViewById<"username">(R.id.username)
val password = findViewById<"password">(R.id.password)
val loginButton = findViewById<"login_button">(R.id.login_button)
loginButton.setOnClickListener {
val username = username.text.toString()
val password = password.text.toString()
if (username == "lukejjones" && password == "test123") {
Toast.makeText(this, "Access Granted", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Access Denied!", Toast.LENGTH_SHORT).show()
}
}
Here is the activity_login.xml code related to this:
EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="textPersonName"
android:padding="20dp"
android:hint="@+string/Username" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:padding="20dp"
android:hint="@+string/Password" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@color/black"
android:text="Login"
android:textColor="@color/white"
android:onClick="handleLoginClick"
/>
答案1
得分: 1
在尖括号之间的内容是您要求查找的 View 类型。它必须是 View 的具体类(或超类)。例如:
val username = findViewById<EditText>(R.id.username)
或者,您也可以这样写:
val username: EditText = findViewById(R.id.username)
我建议您阅读以下两页的 Java 文档,因为您需要基本了解泛型才能在 Kotlin 中做更多事情。由于某种原因,Kotlin 文档没有涵盖这个内容:
除了这两页之外,内容开始更多地涉及到与 Java 特定相关的内容,所以现在可能不要阅读那些内容,以免让自己感到困惑。当您对这个概念更加熟悉时,可以阅读 Kotlin 文档中关于泛型和变异的部分。
英文:
The thing that goes between the angle brackets is the type of View you are requesting it to look for. It has to be the concrete class (or a superclass) of the View. For example:
val username = findViewById<EditText>(R.id.username)
Alternatively, you can write it like:
val username: EditText = findViewById(R.id.username)
I recommend you read these two pages of the Java documentation, because you need a basic understanding of generics to be able to do much of anything in Kotlin. The Kotlin documentation doesn't cover this for some reason:
- Why Use Generics? But with this one, be aware that their example without generics, where they have to cast, cannot exist in Kotlin because Kotlin enforces generics (no raw types). But it still illustrates why generics are used in the first place.
- Generic Types
Beyond those two pages, it starts to get more into Java-specific stuff so maybe don't confuse yourself by reading those for now. When you're more comfortable with the concept, read the Kotlin documentation on generics and variance.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论