FloatingActionButton类无法实例化

时间:2015-07-09 13:34:34

标签: android android-layout

在查看Material Design中的新方法后,我尝试在应用程序中实现浮动操作按钮。但是,我在预览中遇到此错误(预览Android版本21):

The following classes could not be instantiated:
- android.support.design.widget.FloatingActionButton
(First paragraph of error)
java.lang.NullPointerException
at android.support.v4.graphics.drawable.DrawableCompatLollipop.setTintList(DrawableCompatLollipop.java:56)
at android.support.v4.graphics.drawable.DrawableCompat$LollipopDrawableImpl.setTintList(DrawableCompat.java:145)
at android.support.v4.graphics.drawable.DrawableCompat.setTintList(DrawableCompat.java:270)
etc...

任何人都有任何想法?这是我的build.gradle:

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
classpath 'com.android.tools.build:gradle:1.2.3'


defaultConfig {
    applicationId "owensetiawan.tutorials.fab"
    minSdkVersion 15
    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:design:22.2.0'
}

如果有人可以帮助我解决我的问题,那将是非常棒的。

4 个答案:

答案 0 :(得分:5)

嘿,我在实例化设计库时遇到了同样的问题,并通过这个解决了它

步骤1> fab的代码

    <android.support.design.widget.FloatingActionButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom"
      android:clickable="true"
      android:src="@drawable/search"
      app:backgroundTint="@color/color_fav"
      app:borderWidth="0dp"
      android:layout_alignParentBottom="true"
      android:layout_alignParentEnd="true"
      />

步骤2&gt;依赖

    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'
    compile "com.android.support:support-v4:22.2.1"

步骤3&gt;上述问题的主要步骤

简单一个: - 转到您的xml并点击预览,然后在预览栏中选择AP1 21

答案 1 :(得分:4)

确保 AppCompat Library VERSION 设计支持库相同。这主要是一个内部依赖冲突,保持两个版本相同的固定它: -

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

答案 2 :(得分:1)

我有同样的问题更新了“Android支持库”(22.2.1)。阅读我对同类问题的回答。 Cannot setup floating action buttons as class cannot be found (Android Studio)

答案 3 :(得分:0)

我遇到了类似的错误,只需更改Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="victor.com.ec.gastos_diarios">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ActividadPrincipal">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="victor.com.ec.gastos_diarios">

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

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

</manifest>