android - Kotlin Android Extensions 和 AndroidX 似乎没有正确转换 View

标签 android kotlin kotlin-android-extensions androidx

当我尝试将 AndroidX 用于我的 Android/Kotlin 项目时,KAE 恢复的很多 View 在我的代码中都失败了(类型不匹配),因为它们都是用 View! 类型定义的。

例如,这里: enter image description here

或在这里:

enter image description here

发生了什么?

这是我的工具栏所在的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.DrawerLayout
        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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </android.support.design.widget.AppBarLayout>

        <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/framelayout"/>

    </LinearLayout>

    <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/navbar_background"
            app:itemTextColor="#FFF"
            app:itemIconTint="#FFF"
            app:itemBackground="@drawable/drawer_selected_item"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/menu_drawer"/>

</androidx.core.widget.DrawerLayout>

这是我的 Gradle:app :

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "my.app"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0.1"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.core:core-ktx:1.0.0'

    implementation 'androidx.multidex:multidex:2.0.0'

    implementation 'com.google.firebase:firebase-core:16.0.4'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.5'

    implementation "org.jetbrains.anko:anko-commons:0.10.5"
    implementation "org.jetbrains.anko:anko-design:0.10.4"
    implementation "org.jetbrains.anko:anko-sdk25:0.10.5"
    implementation "com.chibatching.kotpref:kotpref:2.5.0"
    implementation "com.chibatching.kotpref:initializer:2.5.0"

    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

    implementation "androidx.room:room-runtime:2.1.0-alpha01"
    annotationProcessor "androidx.room:room-compiler:2.1.0-alpha01"
    kapt "androidx.room:room-compiler:2.1.0-alpha01"

    implementation 'com.stripe:stripe-android:8.0.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.stripe:stripe-android:8.0.0'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'

    implementation 'io.card:android-sdk:5.5.0'

    // Smooch
    implementation 'io.smooch:core:5.14.3'
    implementation 'io.smooch:ui:5.14.3'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'com.google.firebase:firebase-core:16.0.4'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'
    implementation 'androidx.annotation:annotation:1.0.0'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    implementation 'androidx.exifinterface:exifinterface:1.0.0'
    implementation 'androidx.media:media:1.0.0'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.10.0'
}

apply plugin: 'com.google.gms.google-services'

最佳答案

我找到了编辑布局文件的解决方案。 使用 com.google.android.material.appbar.AppBarLayout 而不是 android.support.design.widget.AppBarLayout。或者使用 androidx.appcompat.widget.Toolbar 而不是 android.support.v7.widget.Toolbar

不幸的是,您必须手动完成所有操作...

这是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
        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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <com.google.android.material.appbar.AppBarLayout
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </com.google.android.material.appbar.AppBarLayout>

        <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/framelayout"/>

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/navbar_background"
            app:itemTextColor="#FFF"
            app:itemIconTint="#FFF"
            app:itemBackground="@drawable/drawer_selected_item"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/menu_drawer"/>

</androidx.drawerlayout.widget.DrawerLayout>

关于android - Kotlin Android Extensions 和 AndroidX 似乎没有正确转换 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53012516/

相关文章:

android - Kotlin 合成扩展和几个包含相同的布局

android - 我可以执行多深的 Firestore 查询

android - 在Android中使用kotlin下载文件并将其保存到下载文件夹

Kotlin 单元测试 - 如何模拟 Companion 对象的组件?

lambda - Kotlin 为 lambda 生成内部类

Kotlin - 安全调用运算符的链接。不必要的接线员电话

android - 更新 Room 实体并观察列表

Android Phonegap 屏幕旋转锁杀死了我的应用程序

java - 从 libgdx 项目加载 Activity

java - 找不到类 'kotlin.jvm.internal.DefaultConstructorMarker'