Почему мой findview не работает в kotlin? - PullRequest
0 голосов
/ 05 апреля 2020

В настоящее время я работаю над приложением для одного из моих классов, и, когда я создавал приложение, я столкнулся с ошибкой, сообщив, что у меня есть неразрешенные ссылки.

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>

строк. 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>

Я не понимаю, почему я не могу ссылаться на мои кнопки по их идентификаторам в моей основной деятельности?

Ответы [ 2 ]

0 голосов
/ 05 апреля 2020

но это не java вам не нужно писать findviewbyid, это кнопка

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

просто напишите ID

false_button.setOnClickListener{
}

вы просто напишите идентификатор, который вам не нужно реализовывать это или findview

0 голосов
/ 05 апреля 2020

Когда вы объявляете новый идентификатор в XML, вы должны использовать @+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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...