findViewById让我感到非常烦恼。

huangapple go评论69阅读模式
英文:

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&lt;&quot;username&quot;&gt;(R.id.username)
        val password = findViewById&lt;&quot;password&quot;&gt;(R.id.password)
        val loginButton = findViewById&lt;&quot;login_button&quot;&gt;(R.id.login_button)

        loginButton.setOnClickListener {
            val username = username.text.toString()
            val password = password.text.toString()

            if (username == &quot;lukejjones&quot; &amp;&amp; password == &quot;test123&quot;) {
                Toast.makeText(this, &quot;Access Granted&quot;, Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, &quot;Access Denied!&quot;, Toast.LENGTH_SHORT).show()
            }
        }

Here is the activity_login.xml code related to this:

EditText
            android:id=&quot;@+id/username&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginTop=&quot;10dp&quot;
            android:ems=&quot;10&quot;
            android:inputType=&quot;textPersonName&quot;
            android:padding=&quot;20dp&quot;
            android:hint=&quot;@+string/Username&quot; /&gt;

        &lt;EditText
            android:id=&quot;@+id/password&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:ems=&quot;10&quot;
            android:inputType=&quot;textPassword&quot;
            android:padding=&quot;20dp&quot;
            android:hint=&quot;@+string/Password&quot; /&gt;

        &lt;Button
            android:id=&quot;@+id/login_button&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_margin=&quot;20dp&quot;
            android:background=&quot;@color/black&quot;
            android:text=&quot;Login&quot;
            android:textColor=&quot;@color/white&quot;
            android:onClick=&quot;handleLoginClick&quot;
            /&gt;

答案1

得分: 1

在尖括号之间的内容是您要求查找的 View 类型。它必须是 View 的具体类(或超类)。例如:

val username = findViewById<EditText>(R.id.username)

或者,您也可以这样写:

val username: EditText = findViewById(R.id.username)

我建议您阅读以下两页的 Java 文档,因为您需要基本了解泛型才能在 Kotlin 中做更多事情。由于某种原因,Kotlin 文档没有涵盖这个内容:

  • 为什么使用泛型? 但要注意,其中没有泛型的示例在 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&lt;EditText&gt;(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.

huangapple
  • 本文由 发表于 2023年5月24日 23:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定