java.lang.IllegalArgumentException:您需要将Theme.AppCompat主题(或后代)与设计库一起使用

Fla*_*dun 9 java android android-theme material-design

我试图在本教程的帮助下从Material Design实现"带图像的灵活空间":

带有android设计支持库的工具栏动画

但我在布局预览中收到此渲染问题消息:

无法实例化以下类:

- android.support.design.widget.CoordinatorLayout(Open Class,Show Exception,Clear Cache) - android.support.design.widget.AppBarLayout(Open Class,Show Exception,Clear Cache)

我将Theme.AppCompat主题应用到我的应用程序中,但它会在每个其他活动中搞砸操作栏和外观.此外,它会在操作栏方法上引发错误,例如:

actionBar.setDisplayHomeAsUpEnabled(真);

将错误(粗略地)陈述为:

在null对象引用上调用setDisplayHomeAsUpEnabled(boolean)

此外Theme.AppCompat,在整个应用程序中使用预览屏幕会出现以下错误:

找不到以下类: - android.support.v7.internal.app.WindowDecorActionBar(修复构建路径,创建类)

因此,我不想Theme.AppCompat用于整个应用程序.然而,使用Theme.AppCompat了,我想用"与图像弹性空间"的具体活动设计不能解决CoordinatorLayoutAppBarLayout前面提到的问题.

请告诉我该怎么办!我已经在同一个问题上阅读了很多stackoverflow帖子,但它们对我不起作用!我重新启动了android studio并使我的缓存无效并重启了无数次!!

以下是我的依赖项 build.gradle

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile project(':viewPagerIndicator')
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}
Run Code Online (Sandbox Code Playgroud)

在手机上按原样运行应用程序会出现此错误:

java.lang.IllegalStateException:此Activity已经有一个由窗口装饰提供的操作栏.不要在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false以使用工具栏.

将windowActionBar设置为false,如下所示:

<item name="windowActionBar">false</item>
Run Code Online (Sandbox Code Playgroud)

然后运行该应用程序会抛出此错误:

java.lang.RuntimeException:无法启动活动ComponentInfo {com.seven.actionbar/com.seven.actionbar.EventsDetailActivity}:java.lang.IllegalArgumentException:AppCompat不支持当前主题功能:{windowActionBar:false,windowActionBarOverlay:false ,android:windowIsFloating:false,windowActionModeOverlay:false,windowNoTitle:false}

以下是EventsDetailsActivity.java文件中的工具栏位:

    Toolbar toolbar;
    CollapsingToolbarLayout collapsingToolbar;

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

        actionBarColor =
                new ColorDrawable(ContextCompat.getColor(getApplicationContext(), R.color.ufl_orange));

        tDes = (TextView)findViewById(R.id.evt_desc);
        tVenue = (TextView)findViewById(R.id.evt_venue);
        tDate = (TextView)findViewById(R.id.evt_date);
        tTime = (TextView)findViewById(R.id.evt_time);
        tPdate = (TextView)findViewById(R.id.evt_post_date);
        tPtime = (TextView)findViewById(R.id.evt_post_time);
        tCont = (TextView)findViewById(R.id.evt_contact);
        tOrg = (TextView)findViewById(R.id.evt_org);
        tCount = (TextView)findViewById(R.id.evt_count);

        //Intent intent = getIntent();
        //joinMap = (HashMap)intent.getSerializableExtra("e_uMap");
        myApp = (MyApp)getApplication();

        //action bar magic
        actionBarColor.setAlpha(0);

        toolbar = (Toolbar) findViewById(R.id.anim_toolbar);
        setSupportActionBar(toolbar);
        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        ImageView header = (ImageView) findViewById(R.id.header);

        new LoadDetail().execute();

        goingSwitch = (Switch) findViewById(R.id.btn_join);
        goingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()

        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                new JoinEvents().execute(String.valueOf(isChecked));
            }
        });
    }



    @Override
    public void onStart(){
        super.onStart();
//        actionBar = this.getActionBar();
//        actionBar.setDisplayShowHomeEnabled(false);
    }
Run Code Online (Sandbox Code Playgroud)

这是随之而来的布局代码:

<?xml version="1.0" encoding="utf-8"?>

<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=".EventsDetailActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <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:expandedTitleMarginBottom="32dp"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/monalisa"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>'

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

ScrollView和相关的子元素(这里没有显示)是页面的主要内容.

还将主题修改为:

<style name="EventsTheme" parent="Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

虽然IllegalStateExceptionRuntimeException都没有了,我不能看到CoordinatorLayout一次.

Int*_*iya 4

你的 logcat 抛出什么

java.lang.IllegalStateException:此 Activity 已经具有由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。

  1. 您的所有主题(需要Action Bar/Toolbar)必须继承自Theme.AppCompat。有多种可用变体,包括 Light 和 NoActionBar。

请阅读AppCompat官方文档

你该怎么办

  1. 请设置parent="Theme.AppCompat.NoActionBar"而不是 parent =“Theme.AppCompat.Light

  2. 避免重复的库调用。support:appcompat在您所在的区域打了两次电话build.gradle

您可以在SO上查看类似类型的问题

  1. Window.FEATURE_ACTION_BAR 并将 windowActionBar 设置为 false
  2. AppCompat 不支持当前主题功能


归档时间:

查看次数:

14581 次

最近记录:

9 年,4 月 前