浮动操作按钮throing错误

问题描述:

我真的不知道为什么我的程序没有执行。我觉得我做everythink这里解释:FloatingActionButton example with Support Library浮动操作按钮throing错误

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') 
    } 

添加@色/ floating_action_button_color到\应用程序数据\本地\的Android \ Android的SDK \平台\ 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> 

任何想法如何解决这一问题?谢谢。

+0

你能发布MyLayoutActivity的代码吗? – jayeshsolanki93

+0

错误是:没有找到类“android.support.design.widget.CoordinatorLayout”。尝试再次同步您的build.gradle并重建项目... – W0rmH0le

+0

清理您的项目并重新启动。 – jaibatrik

确定....我切换到Android工作室...哇一切工作。

您的MyLayoutActivity应扩展AppCompactActivity而不是ActionBarActivity

+0

对不起,我忘了写“我的_”......它在eclipse中是正确的......改为AppCompactActivity ..但仍然是一样的错误。 – h0ppel

+0

@ h0ppel现在尝试重新启动重建项目 – jayeshsolanki93

+0

没有改善:( – h0ppel

刚刚尝试n将项目父主题:

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

仍错误 – h0ppel

+0

正如我提到的那样添加AppCompatActivity和Theme.Appcompat。同时更新ti最新版本。它应该工作 – prat