android.view.InflateException:输出类android.support.design.widget.CoordinatorLayout时出错

时间:2016-01-07 21:03:48

标签: java android

我想显示一个SnackBar和一个FloatingActionButton,所以我将android-support-design.jar添加到了我的项目中。但是我收到了以下错误:

 01-07 20:31:31.799: E/AndroidRuntime(929): FATAL EXCEPTION: main
01-07 20:31:31.799: E/AndroidRuntime(929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.KM.example/com.KM.example.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.design.widget.FloatingActionButton
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.os.Looper.loop(Looper.java:123)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 20:31:31.799: E/AndroidRuntime(929):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 20:31:31.799: E/AndroidRuntime(929):  at java.lang.reflect.Method.invoke(Method.java:507)
01-07 20:31:31.799: E/AndroidRuntime(929):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 20:31:31.799: E/AndroidRuntime(929):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 20:31:31.799: E/AndroidRuntime(929):  at dalvik.system.NativeStart.main(Native Method)
01-07 20:31:31.799: E/AndroidRuntime(929): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.design.widget.FloatingActionButton
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
01-07 20:31:31.799: E/AndroidRuntime(929):  at com.KM.example.MainActivity.onCreate(MainActivity.java:17)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-07 20:31:31.799: E/AndroidRuntime(929):  ... 11 more
01-07 20:31:31.799: E/AndroidRuntime(929): Caused by: java.lang.ClassNotFoundException: android.support.design.widget.FloatingActionButton in loader dalvik.system.PathClassLoader[/data/app/com.KM.example-2.apk]
01-07 20:31:31.799: E/AndroidRuntime(929):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
01-07 20:31:31.799: E/AndroidRuntime(929):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
01-07 20:31:31.799: E/AndroidRuntime(929):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.view.LayoutInflater.createView(LayoutInflater.java:471)
01-07 20:31:31.799: E/AndroidRuntime(929):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
01-07 20:31:31.799: E/AndroidRuntime(929):  ... 20 more

我的活动是:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                Snackbar.make(view, "Snackbar", Snackbar.LENGTH_LONG).show();
              }
            });

    }
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    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="end|bottom"/>

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

styles.xml:

<resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">

    </style>

    <style name="AppTheme" parent="AppBaseTheme">

    </style>

</resources>

我发现了一些类似的问题,但没有一个解决方案适合我。 有人可以帮我这个吗?非常感谢你。

1 个答案:

答案 0 :(得分:0)

  1. 将设计库的目标更改为23
  2. 从中导入支持设计库 \sdk\extras\android\support\design to eclipse workspace
  3. 打开AppCompat的project.properties并设计库和目标 它到23
  4. 清理完整工作区
  5. 运行
  6. 希望有所帮助