Java Kotlin无法识别Android XML元素的ID
各位程序员好 我尝试从xml文件中调用元素示例:我的activity.xml中的一个按钮,但是当我尝试在代码中调用它时,MainActivity.kt没有解析我的id 我试图清理和重建项目,但仍然有问题,也无效缓存重新启动 像这样,我想从acitivity_main.xml调用一个按钮 button.setOnClickListener{ } 按钮。我试着按一下按钮,但什么也没显示//Java Kotlin无法识别Android XML元素的ID,java,android,kotlin,Java,Android,Kotlin,各位程序员好 我尝试从xml文件中调用元素示例:我的activity.xml中的一个按钮,但是当我尝试在代码中调用它时,MainActivity.kt没有解析我的id 我试图清理和重建项目,但仍然有问题,也无效缓存重新启动 像这样,我想从acitivity_main.xml调用一个按钮 button.setOnClickListener{ } 按钮。我试着按一下按钮,但什么也没显示// } } xml活动性: <?xml version="1.0" enco
}
}
xml活动性:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="138dp"
android:layout_height="58dp"
android:layout_marginStart="136dp"
android:layout_marginEnd="137dp"
android:layout_marginBottom="212dp"
android:text="أظهار النتيجة"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="411dp"
android:layout_height="360dp"
android:layout_marginStart="43dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="43dp"
android:ems="10"
android:inputType="textPersonName"
android:text="ادخل النص هنا"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="417dp"
android:layout_height="195dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/result"
android:text="النتائج:"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
在顶部添加以下导入语句
import kotlinx.android.synthetic.main. activity_main.*
在顶部添加以下导入语句
import kotlinx.android.synthetic.main. activity_main.*
确保在gradle文件的顶部有这两行
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
放
导入kotlinx.android.synthetic.main.activity_main.*在您的活动文件中确保您的gradle文件顶部有这两行
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
放
导入kotlinx.android.synthetic.main.activity_main.*现在,在您的活动文件中,插件部分如下所示:
现在,插件部分如下所示:
有一种更好的方法称为视图绑定。这让事情变得容易多了。当你使用它时,你不需要“kotlin android扩展”
有一种更好的方法称为视图绑定。这让事情变得容易多了。当你使用它时,你不需要“kotlin android扩展”
我不认为你可以使用合成导入作为字段变量,尝试将其移动到onCreate methodnothing Change中。我不认为你可以使用合成导入作为字段变量,尝试将其移动到onCreate methodnothing Changed中。不要发布代码、数据、错误消息等的图像。将文本复制或键入问题中不要张贴代码、数据、错误消息等的图像-在问题中复制或键入文本-