【问题标题】:java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design libraryjava.lang.IllegalArgumentException:您需要将 Theme.AppCompat 主题(或后代)与设计库一起使用
【发布时间】:2016-02-17 22:32:53
【问题描述】:

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

Toolbar animation with android design support library

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

以下类无法实例化:

- android.support.design.widget.CoordinatorLayout(打开类,显示异常,清除缓存) - android.support.design.widget.AppBarLayout(打开类,显示异常,清除缓存)

我将Theme.AppCompat 主题应用于我的应用程序,但它搞砸了操作栏和其他所有活动的外观。 它还会在操作栏方法上引发错误,例如:

actionBar.setDisplayHomeAsUpEnabled(true);

将错误(大致)表述为:

正在对空对象引用调用 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'
}

在我的手机上按原样运行应用程序给我这个错误:

java.lang.IllegalStateException: 这个 Activity 已经有一个动作 酒吧提供的窗户装饰。不要要求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 您的主题改为使用工具栏。

像这样将 windowActionBar 设置为 false :

<item name="windowActionBar">false</item>

然后运行应用程序会引发此错误:

java.lang.RuntimeException:无法启动活动 组件信息{com.seven.actionbar/com.seven.actionbar.EventsDetailActivity}: java.lang.IllegalArgumentException:AppCompat 不支持 当前主题功能:{ windowActionBar: false, windowActionBarOverlay:假,android:windowIsFloating:假, 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);
    }

这是与之配套的布局代码:

<?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>

ScrollView 和相关的子元素(此处未显示)是页面的主要内容。

还将主题修改为:

<style name="EventsTheme" parent="Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>

虽然IllegalStateExceptionRuntimeException 消失了,但我再也看不到CoordinatorLayout

【问题讨论】:

  • 您是否将您的支持设计库添加为库项目
  • 是的,我做到了。我已经更新了我的问题。
  • 尝试将布局预览更改为关于 Api-19 的较低 Api
  • 更改预览并不是真正的问题。当我在手机上上传应用程序时,我在活动中看不到 CoordinatorLayout 部分。
  • 更新了有关按原样运行应用程序的详细信息的问题。

标签: java android material-design android-theme


【解决方案1】:

你的 logcat 抛出了什么

java.lang.IllegalStateException: 这个 Activity 已经有一个动作 酒吧提供的窗户装饰。不要要求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 您的主题改为使用工具栏。

  1. 您的所有主题(需要操作栏/工具栏)都必须继承 来自 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 and set windowActionBar to false
  2. AppCompat does not support the current theme features

【讨论】:

【解决方案2】:

好的!著名的工具栏问题。

从表面上看,尝试对您发布的代码进行一些更改。

1)如果您希望 ActionBar 在除此之外的其他 Activity 中使用,请为 NoActionBar 创建新样式并将该样式添加到此 Activity

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

你没有AndroidManifest.xml,分配这个主题以便只有EventsDetailsActivity没有有ActionBar,而rest 会有。

<activity
        android:name="EventsDetailsActivity"
        android:theme="@style/AppTheme.NoActionBar">

2) ScrollView 不适用于CoordinatorLayout。所以为了实现‘Flexible Space with Image‘尝试NestedScrollView而不是ScrollView。这将为您提供适当的动画效果。

3) 在您的EventsDetailsActivity 中将ActionBar 更改为Toolbar

希望对你有帮助。

【讨论】:

    【解决方案3】:

    为什么需要使用Toolbar?你需要支持一些旧的Android版本吗? 关键是Toolbar 实际上并不是ActionBar。它缺乏一些功能,并迫使您在活动中使用明确的主题。所以如果你不需要支持一些老版本的Android(v7.Toolbar is for SDK 7 (Android 2.1).

    哦,顺便说一句:你想要做的就是折叠工具栏布局。检查此链接以获取教程 http://antonioleiva.com/collapsing-toolbar-layout/

    附:不要编译同一个库的不同版本!不要这样做:

    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'
    

    改为:

    compile 'com.android.support:appcompat-v7:23.0.1'
        compile project(':viewPagerIndicator')
        compile 'com.android.support:design:23.0.1'
    

    【讨论】:

    • "为什么需要使用Toolbar?需要支持一些旧的Android版本吗?" Toolbar 是在 API 级别 21 中添加的。所以请回复,您认为我们应该使用什么来代替 Toolbar ;如果我们不必支持 21 的旧版本
    • stackoverflow.com/questions/29813474/…的讨论中,建议使用Toolbar而不是ActionBar
    • 我在 1.5 年前给出了答案,当 AppBarLayout 出现时,我重新考虑了自己的立场。所以这些天我自己在使用工具栏。不过,如果你不想为你的活动设置 AppCompat 主题,你会问怎么办。
    • 是的,谷歌改变了很多......现在是桥下的水
    猜你喜欢
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多