以下类无法实例化android.support.design.widget.CoordinatorLayout

时间:2016-03-25 05:56:47

标签: android xml android-layout android-studio material-design

我在抽屉布局中使用协调器布局。我想显示一个自动隐藏工具栏并在其中使用电影海报,当滚动电影细节时,它会崩溃。我的应用程序也使用抽屉布局。这是我的布局XML activity_coordinator_layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical"
        tools:context="com.inov8.falconpayconsumer.activities.MovieInfo">

        <RelativeLayout
            android:id="@+id/headerLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/red">

            <include
                android:id="@+id/header_layout2"
                layout="@layout/header" />
        </RelativeLayout>

        <include
            android:id="@+id/sub_header"
            layout="@layout/subheader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/headerLayout"
            android:visibility="visible" />

    </LinearLayout>

    <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:fitsSystemWindows="true"
       tools:context="com.inov8.falconpayconsumer.activities.bookme.MovieDetailsActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    android:minHeight="100dp"
                    android:scaleType="matrix"
                    android:src="@drawable/gods_of_egypt"
                    app:layout_collapseMode="parallax" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin">

                </android.support.v7.widget.Toolbar>

            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

                    <TextView
                        android:id="@+id/description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="15dp"
                        android:text="@string/lorem_ipsum"
                        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

                </LinearLayout>

            </FrameLayout>

        </android.support.v4.widget.NestedScrollView>

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

    <include
        android:id="@+id/header_layout"
        layout="@layout/drawer_layout_list" />
</android.support.v4.widget.DrawerLayout>

如果它是应用程序默认主题中的问题,那么它是<style name="My_App_Theme" parent="Theme.AppCompat.NoActionBar" /> 在清单中我使用android:theme="@style/AppTheme"

以下是绘制此布局时显示的完整错误

Rendering Problems The following classes could not be instantiated:     - android.support.design.widget.CoordinatorLayout (Open Class, Show Exception, Clear Cache)    
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE  Exception Details 
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:34)   at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:182)   at 
android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:176)   at 
java.lang.reflect.Constructor.newInstance(Constructor.java:423)   at 
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)   at 
android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:835)   at 
android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)   at 
android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)   at 
android.view.LayoutInflater.inflate(LayoutInflater.java:515)   at 
android.view.LayoutInflater.inflate(LayoutInflater.java:394)

1 个答案:

答案 0 :(得分:1)

第一个。

  

类无法实例化   android.support.design.widget.CoordinatorLayout

您已在gradle文件中添加此依赖项。

dependencies {

    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
}

并且第二件事。

  

IllegalArgumentException:您需要使用Theme.AppCompat主题(或   后裔)

您需要做的只是将android:theme="@style/Theme.AppCompat.Light"添加到AndroidManifest.xml文件中的应用标记。