为什么findviewbyid在Kotlin中不起作用?

问题描述 投票:0回答:2

[我目前正在为我的一个班级开发应用程序,而在构建应用程序时,我遇到一个错误,说我有未解决的引用。

MainActivity.kt

package com.example.mobileappcomp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private lateinit var trueButton: Button
    private lateinit var falseButton: Button

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

        trueButton = findViewById(R.id.true_button)
        falseButton = findViewById(R.id.false_button)

        trueButton.setOnClickListener() {

        }

        falseButton.setOnClickListener() {

        }
    }
}

activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/question_text" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id=/true_button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id=/false_button" />
    </LinearLayout>
</LinearLayout>

strings.xml

<resources>
    <string name="app_name">MobileAppComp</string>
    <string name="question_text">The CPU is responsible for executing instructions for the computer</string>
    <string name="true_button">True</string>
    <string name="false_button">False</string>
    <string name="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect!</string>
</resources>

我不明白为什么我不能在我的mainactivity中通过按钮的ID来引用我的按钮?

kotlin reference id
2个回答
0
投票

当用XML声明新ID时,应使用@+id/,而不是使用@+id=/

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/question_text" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id/true_button" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id/false_button" />
    </LinearLayout>
</LinearLayout>

0
投票

但不是Java,您无需编写findviewbyid这是一个按钮

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id=/false_button" />

只写ID

false_button.setOnClickListener{
}

您只需输入ID您不需要实现它或findview

© www.soinside.com 2019 - 2024. All rights reserved.