【问题标题】:Error while adding Floating Action Button in android在android中添加浮动操作按钮时出错
【发布时间】:2019-07-14 21:50:39
【问题描述】:

当我从调色板添加浮动操作按钮时,错误显示如下 “对象不是声明类的实例” “渲染过程中引发异常:对象不是声明类的实例(详细信息)”

我试过迁移到androidX还是不行

这是我的模块级 gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.vignesh.layoutlearn"
        minSdkVersion 27
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.android.material:material:1.1.0-alpha07'
    implementation 'com.android.support:design:28.0.0'
    implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    implementation 'com.google.android.material:material:1.0.0'
}

我删除了材料设计库,现在它工作正常,但我想知道为什么会这样。如果我需要材料设计和制造怎么办?

【问题讨论】:

  • 它是在编辑器中显示,还是在您编译项目时显示?
  • 它在属性按钮附近显示红色图标。我删除了材料设计库,现在它可以正常工作了,但是为什么呢?
  • 您删除了材料设计库,您可以使用android.support.design.widget.FloatingActionButton

标签: android android-studio android-layout


【解决方案1】:

我建议你去 File > Invalidate Caches/Restart

如果您使用的是支持库 implementation 'com.android.support:design:28.0.0'

<android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:srcCompat="@tools:sample/avatars"/>

如果您使用的是 AndroidX 版本implementation 'com.google.android.material:material:1.0.0'

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:srcCompat="@tools:sample/avatars"/>

这应该可行..

【讨论】:

    【解决方案2】:

    尝试将此行添加到您的依赖项:

    implementation 'com.android.support:design:28.0.0'
    

    应与目标 SDK 版本相同。

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 2023-03-15
      相关资源
      最近更新 更多