浮动操作按钮throing错误

时间:2016-05-31 15:46:49

标签: java android

我真的不知道为什么我的程序没有执行。我想我在这里解释了每一个想法:FloatingActionButton example with Support Library

my_layout.xml

<android.support.design.widget.CoordinatorLayout 
    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:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

的gradle:

apply plugin: 'com.android.application'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':appcompat_v7')
     compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }

将@ color / floating_action_button_color添加到\ AppData \ Local \ Android \ android-sdk \ platforms \ android-22 \ data \ res \ values \ themes.xml

但执行程序后总会出现此错误:

05-31 15:16:11.792: E/AndroidRuntime(1181): FATAL EXCEPTION: main
05-31 15:16:11.792: E/AndroidRuntime(1181): Process: com.hppl.accountbook, PID: 1181
05-31 15:16:11.792: E/AndroidRuntime(1181): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hppl.accountbook/com.hppl.accountbook.MyLayoutActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.app.ActivityThread.access$800(ActivityThread.java:151)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.os.Looper.loop(Looper.java:135)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.app.ActivityThread.main(ActivityThread.java:5257)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at java.lang.reflect.Method.invoke(Native Method)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at java.lang.reflect.Method.invoke(Method.java:372)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-31 15:16:11.792: E/AndroidRuntime(1181): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at com.hppl.accountbook.MyLayoutActivity.onCreate(MyLayoutActivity.java:13)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.app.Activity.performCreate(Activity.java:5990)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
05-31 15:16:11.792: E/AndroidRuntime(1181):     ... 10 more
05-31 15:16:11.792: E/AndroidRuntime(1181): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.CoordinatorLayout" on path: DexPathList[[zip file "/data/app/com.hppl.accountbook-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
05-31 15:16:11.792: E/AndroidRuntime(1181):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.view.LayoutInflater.createView(LayoutInflater.java:571)
05-31 15:16:11.792: E/AndroidRuntime(1181):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
05-31 15:16:11.792: E/AndroidRuntime(1181):     ... 19 more
05-31 15:16:11.792: E/AndroidRuntime(1181):     Suppressed: java.lang.ClassNotFoundException: android.support.design.widget.CoordinatorLayout
05-31 15:16:11.792: E/AndroidRuntime(1181):         at java.lang.Class.classForName(Native Method)
05-31 15:16:11.792: E/AndroidRuntime(1181):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
05-31 15:16:11.792: E/AndroidRuntime(1181):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
05-31 15:16:11.792: E/AndroidRuntime(1181):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
05-31 15:16:11.792: E/AndroidRuntime(1181):         ... 22 more
05-31 15:16:11.792: E/AndroidRuntime(1181):     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

主要:

   import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;

public class MyLayoutActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_layout, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

styles.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->

    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

知道怎么解决这个问题吗?感谢。

3 个答案:

答案 0 :(得分:0)

您的MyLayoutActivity应该延长AppCompactActivity而不是ActionBarActivity

答案 1 :(得分:0)

尝试将项目父主题更改为:

<style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

答案 2 :(得分:0)

好吧....我切换到Android Studio ......哇,一切正常。