Objekte werden nicht gefunden

  • Antworten:11
  • Bentwortet
Robbiani Renato
  • Forum-Beiträge: 602

30.10.2020, 21:20:53 via Website

Hallo zusammen

Ich komme in meinem Projekt mit Kotline nicht mehr weiter. Ich habe mein Layout erstellt. Nun möchte ich dem Layout leben einhauchen. Aber in der "MainActivity.kt" werden die Objekte vom Layout einfach nicht gefunden. Was läuft hier schief?

Layout :

<?xml version="1.0" encoding="utf-8"?>

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lay_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<ScrollView
    android:id="@+id/scr_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/lay_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/lay_eins"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_null"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="0" />

            <Button
                android:id="@+id/btn_eins"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1" />

            <Button
                android:id="@+id/btn_zwei"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2" />

            <Button
                android:id="@+id/btn_drei"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3" />

            <Button
                android:id="@+id/btn_vier"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="4" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/lay_zwei"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_fuenf"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="5" />

            <Button
                android:id="@+id/btn_sechs"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="6" />

            <Button
                android:id="@+id/btn_sieben"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="7" />

            <Button
                android:id="@+id/btn_acht"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="8" />

            <Button
                android:id="@+id/btn_neun"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="9" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/lay_drei"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_zehn"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="10" />

            <Button
                android:id="@+id/btn_zwanzig"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="20" />

            <Button
                android:id="@+id/btn_dreisig"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="30" />

            <Button
                android:id="@+id/btn_vierzig"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="40" />

            <Button
                android:id="@+id/btn_fuenfzig"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="50" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/lay_vier"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_sechzig"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="60" />

            <Button
                android:id="@+id/btn_siebzieg"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="70" />

            <Button
                android:id="@+id/btn_achzig"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="80" />

            <Button
                android:id="@+id/btn_neunzig"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="90" />

            <Button
                android:id="@+id/btn_hundert"
                style="@style/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="100" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/lay_preis"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/lbl_preis"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="5"
                android:text="@string/lbl_preis_text" />

            <TextView
                android:id="@+id/txt_preis"
                style="@style/label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="20"
                android:hint="@string/lbl_preis_text" />

            <ImageButton
                android:id="@+id/btn_plus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackground"
                app:srcCompat="@drawable/button_plus_klein" />

            <ImageButton
                android:id="@+id/btn_minus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackground"
                app:srcCompat="@drawable/button_minus_klein" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/lay_summe"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/lbl_summe"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/lbl_summe_text" />

            <TextView
                android:id="@+id/txt_summe"
                style="@style/label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="@string/lbl_summe_text" />

        </LinearLayout>
    </LinearLayout>
</ScrollView>

MainActivity :

package ch.robbisoft.kassieren

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

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


}

}

Manifest :

<?xml version="1.0" encoding="utf-8"?>

package="ch.robbisoft.kassieren">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Gruss Renato

Kommentieren
Beste Antwort
Robbiani Renato
  • Forum-Beiträge: 602

31.10.2020, 15:52:34 via Website

Hallo zusammen

Ich habe es hin gekriegt. Ich habe folgendes gemacht :

In build.gradel habe ich folgende Einträge gelöscht.

plugins {
id 'com.android.application'
id 'kotlin-android'

}

Dann habe ich ganz oben folgende Zeilen eingetragen

apply plugin: "com.android.application"
apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"

Dann habe ich in MainActivity.kt diese Zeile eingefügt.

import kotlinx.android.synthetic.main.activity_main.*

Und nun kann ich das Projekt wieder bearbeiten und starten.

Gruss Renato

Hilfreich?
Kommentieren
Jokel
  • Forum-Beiträge: 1.527

30.10.2020, 21:38:28 via Website

Hallo
Ja wo erstellst du denn einen listener?
Oder wo in deiner Activity greifst du auf etwas zu. Da ist nichts in deiner onCrate.

Entweder du suchst Objekte wie in Java mit findviewbyId.
Oder
Dies hier.
https://antonioleiva.com/kotlin-android-extensions/

— geändert am 31.10.2020, 08:53:57

Hilfreich?
Kommentieren
Robbiani Renato
  • Forum-Beiträge: 602

31.10.2020, 11:11:25 via Website

Ciao Jokel

Genau da liegt mein Problem. Die Zeile

import kotlinx.android.synthetic.main.activity_main.*

Führt bei mir zu einem Fehler (Unresolved reference: kotlinx). Dieses Problem habe ich seit dem ich auf Version 4 upgedatet habe. Beim erstellen von einem neuen Projekt läuft etwas schief. Ich weiss nicht ob ich was falsch mache oder Android-Studio?

Gruss Renato

Hilfreich?
Kommentieren
Robbiani Renato
  • Forum-Beiträge: 602

31.10.2020, 12:16:15 via Website

Ciao Jokel

Hilft alles nicht. Ich denke, dass da in Android-Studio Version 4 ein Bug ist. Bisher ist es ohne Probleme gegangen. Ich musste nie was im build.gradel ändern. Ich warte mal auf das nächste update.

Gruss Renato

Hilfreich?
Kommentieren
Jokel
  • Forum-Beiträge: 1.527

31.10.2020, 12:20:03 via Website

Wir sind aber auch schon bei 4.1

Die Zeit von 4.0 zu 4.0.1 war nur kurz.
Von Mai bis Juni.

4.1 ab August

Für Kotlin kommt ständig was neues also öfters prüfen.

— geändert am 31.10.2020, 12:36:52

Hilfreich?
Kommentieren
Beste Antwort
Robbiani Renato
  • Forum-Beiträge: 602

31.10.2020, 15:52:34 via Website

Hallo zusammen

Ich habe es hin gekriegt. Ich habe folgendes gemacht :

In build.gradel habe ich folgende Einträge gelöscht.

plugins {
id 'com.android.application'
id 'kotlin-android'

}

Dann habe ich ganz oben folgende Zeilen eingetragen

apply plugin: "com.android.application"
apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"

Dann habe ich in MainActivity.kt diese Zeile eingefügt.

import kotlinx.android.synthetic.main.activity_main.*

Und nun kann ich das Projekt wieder bearbeiten und starten.

Gruss Renato

Hilfreich?
Kommentieren
Jokel
  • Forum-Beiträge: 1.527

31.10.2020, 16:49:59 via Website

Also ist ist es nicht die android studio version gewesen.

Habe ich dir ja auch schon gesagt.
Das du das das Plugin brauchst.

Hilfreich?
Kommentieren
Robbiani Renato
  • Forum-Beiträge: 602

31.10.2020, 17:06:53 via Website

Ciao Jokel

Indirekt schon. Denn mit der Version 3 konnte ich ein neues Kotlin-Projekt erstellen und gleich verwenden. Mit Version 4 wurde im Plugin was geändert. Dies funktioniert aber nicht. Darum muss man das Projekt so anpassen, dass es wieder geht.

Gruss Renato

Hilfreich?
Jokel
Kommentieren
Jokel
  • Forum-Beiträge: 1.527

31.10.2020, 17:17:03 via Website

Ich musste bei allen meinen Kotlin Projekten die Plugins anpassen auch mit AS 3.x

Hilfreich?
Kommentieren
Robbiani Renato
  • Forum-Beiträge: 602

31.10.2020, 17:46:49 via Website

Ah ja! Ich nicht.

Hilfreich?
Kommentieren
Jokel
  • Forum-Beiträge: 1.527

31.10.2020, 20:01:58 via Website

Habe es gerade mit der Preview release Android Studio 4.2 Canary 15 getest da geht es ohne Probleme.
kein einfügen der drei Zeilen mehr notwendig.

das neue Plugin scheint da zu laufen. Es wird da aber auch gradle 6.7 benutzt.

Hilfreich?
Kommentieren