Binding mit Kotlin

maksimilian

Mitglied
Hallo Ihr,
beim Anlegen eines Projekts stoße ich immer wieder auf das Problem des Binding. Mit welcher Methode kann ich in MainActivity.kt immer auf Objekte in activity_main.xml zugreifen ? Ich möchte das an folgendem einfachen Beispiel demonstrieren, bei welchem auf der Oberfläche ein Button und eine TextView angelegt sind.

[CODE lang="java" title="activity_main.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">

<TextView
android:id="@+id/Ausgabe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/Klick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="164dp"
tools:layout_editor_absoluteY="90dp" />

</androidx.constraintlayout.widget.ConstraintLayout>[/CODE]

[CODE lang="java" title="MainActivity.kt"]package com.example.testbinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.testbinding.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
B = ActivityMainBinding.inflate(layoutInflater)
}
B.Klick.setOnClickListener
{
B.Ausgabe.text = "Klick betaetigt"
}
}
[/CODE]

[CODE lang="java" title="Build Output"]failed
:app:compileDebugKotlin
app/src/main/java/com/example/testbinding/MainActivity.kt
Expecting member declaration
Expecting member declaration
Expecting member declaration
Expecting member declaration
Expecting member declaration
Expecting member declaration
Unresolved reference: databinding
Unresolved reference: B
Unresolved reference: ActivityMainBinding
Function declaration must have a name
Unresolved reference: B
Variable expected[/CODE]

[CODE lang="java" title="build.gradle Projekt"]// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.31"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}[/CODE]

[CODE lang="java" title="build.gradle Module"]plugins {
id 'com.android.application'
id 'kotlin-android'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.example.testbinding"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}[/CODE]

Wie erreiche ich den Zugriff auf Button Klick und TextView Ausgabe in MainActivity.kt ? Im Internet kursieren verschiedene (historische ?) Lösungen, die mich verwirren. Es muss doch ein Standard-Verfahren geben.

Ich verwende Android Studio 4.1.2 in Linux (Mint).

maksimilian
 
Ähnliche Java Themen

Ähnliche Java Themen

Neue Themen


Oben