无法设置浮动操作按钮,因为找不到类(Android Studio)

10

我对Android开发还比较新,正在尝试使用Android Design Support Library这篇指南在Android Studio中设置一个浮动操作按钮。

我的项目:注意到的build.gradle文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'

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

allprojects {
    repositories {
        jcenter()
    }
}

我的模块:app build.gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.noted"
        minSdkVersion 21
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:design:22.2.0'
}

在我的activity_main.xml中,我尝试实现

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

但是在构建后,我遇到了一个渲染问题弹窗,上面写着:

找不到以下类: - android.support.design.widget.FloatingActionButton

我的猜测是我在依赖方面做错了什么,但我真的不知道。我已经尝试了几个小时来解决这个问题,但没有成功,我真的很感激任何帮助。

谢谢!


@SamDozor 是的,我相信如此。在“Extras”下的SDK管理器中,它显示我拥有Rev 15 ASRepository和Rev 22.2 ASLibrary。 - Jtaks
请注意,这个FloatingActionButton在API 21及以上的版本中不可用。有关更多信息,请参见此帖子:https://dev59.com/kV0a5IYBdhLWcg3wNWbA#30609927 - Anggrayudi H
@AnggrayudiH,您能解释一下为什么它在API 21及更高版本上无法工作吗?您链接的帖子只显示了一种替代方案。为什么Google会发布适用于所有Android 2.1或更高版本设备的Android Design Support Library,但却排除了他们最近的发布(Lollipop)? - Jtaks
@AnggrayudiH 这并没有解决我的问题,因为无法找到该类。不过还是谢谢你的提示! - Jtaks
你是否已经对Activity执行了setContentView()或对Fragment执行inflater.inflate(),用于将浮动按钮放置在XML文件中? - Kaveesh Kanwal
显示剩余4条评论
7个回答

11

您需要库的设计,在build.gradle文件中添加:

compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:design:25.1.1'

然后同步Gradle,进行构建并刷新


10

2
很遗憾,那并没有解决问题。 - Jtaks
3
请检查gradle文件中的构建工具版本,确保您已将最新的工具添加到其中。我正在使用"com.android.tools.build:gradle:1.2.3"。 - Niroshan
谢谢@NiroshanChandrawijayakumar。那对我真的很有帮助。 - Sathya Baman

2

最新版本的Android Studio 3.1.2似乎不喜欢在build.gradle文件中使用compile。相反,我使用了implementation,它可以正常工作。

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

2
我最近遇到了这个问题,尝试更改依赖版本、无效缓存和重启等方法。预览窗口渲染了一次,然后重新打开我的xml文件,又出现了同样的错误。
在预览窗口上点击刷新按钮解决了我的问题(暂时),让我能够正确地看到布局及其元素。 XML文件中的预览窗口

1
如果您正在使用androidx,您需要使用以下内容:

implementation 'com.google.android.material:material:1.2.1'

(或者您想要的最新版本)。
XML中的操作按钮如下所示:
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/delete_buttom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_margin="20dp"
        android:src="@drawable/ic_baseline_delete_24px" />

0
我在使用Android Studio时遇到了同样的问题,它抱怨无法实例化FloatingActionButton。"异常详情"显示如下:
java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
    at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
    at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:159)
    at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:153)

基本上,我一直在使用Material.Light主题,但是我需要使用AppCompat主题。我切换到了AppCompat.Light主题,问题得到了解决!


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接