java.lang.IllegalArgumentException:您需要将 Theme.AppCompat 主题(或后代)与设计库一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33741954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library
提问by Flame of udun
I am trying to implement "Flexible Space with Image" from Material Design with the help of this tutorial:
我正在尝试在本教程的帮助下从 Material Design 实现“带图像的灵活空间”:
Toolbar animation with android design support library
But I am getting this Rendering problem message in the layout preview :
但我在布局预览中收到此渲染问题消息:
The following classes could not be instantiated:
-?android.support.design.widget.CoordinatorLayout (Open Class, Show Exception, Clear Cache) -?android.support.design.widget.AppBarLayout (Open Class, Show Exception, Clear Cache)
无法实例化以下类:
-?android.support.design.widget.CoordinatorLayout (Open Class, Show Exception, Clear Cache) -?android.support.design.widget.AppBarLayout (Open Class, Show Exception, Clear Cache)
I applied the Theme.AppCompat
theme to my application but it screws up the action bar and appearance in every other activity.
Also it throws an error on action bar methods such as :
我将Theme.AppCompat
主题应用于我的应用程序,但它在所有其他活动中搞砸了操作栏和外观。它还会在操作栏方法上引发错误,例如:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
stating the error(roughly) as :
将错误(大致)说明为:
setDisplayHomeAsUpEnabled(boolean) is being called on a null object reference
正在对空对象引用调用 setDisplayHomeAsUpEnabled(boolean)
Additionally using Theme.AppCompat
for the entire application gives the following error in the preview screen :
此外,Theme.AppCompat
用于整个应用程序会在预览屏幕中出现以下错误:
The following classes could not be found: -?android.support.v7.internal.app.WindowDecorActionBar (Fix Build Path, Create Class)
找不到以下类:-?android.support.v7.internal.app.WindowDecorActionBar (Fix Build Path, Create Class)
Thus I don't want to use Theme.AppCompat
for the entire application. However, using a Theme.AppCompat
for the specific activity where I want to use the "Flexible space with image" design doesn't resolve the CoordinatorLayout
and AppBarLayout
issue stated earlier.
因此我不想Theme.AppCompat
用于整个应用程序。然而,使用Theme.AppCompat
了,我想用“与图像弹性空间”的具体活动设计没有解决CoordinatorLayout
和AppBarLayout
问题如前所述。
Please tell me what to do! I have read many stackoverflow posts on the same issue but they didn't work for me! I have restarted android studio and invalidated my cache and restarted countless times as well!!
请告诉我应该怎么做!我已经阅读了许多关于同一问题的 stackoverflow 帖子,但它们对我不起作用!我已经重新启动了 android studio 并使我的缓存无效并重新启动了无数次!!
Here are the dependencies in my build.gradle
这是我的依赖项 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'
}
Running the app as is on my phone gives me this error:
在我的手机上按原样运行应用程序会给我这个错误:
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
java.lang.IllegalStateException:此活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。
Setting the windowActionBar to false like so :
像这样将 windowActionBar 设置为 false :
<item name="windowActionBar">false</item>
and then running the app throws this error :
然后运行该应用程序会引发此错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.seven.actionbar/com.seven.actionbar.EventsDetailActivity}: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }
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 }
Here is the toolbar bit from the EventsDetailsActivity.java file :
这是 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);
}
Here is the layout code that goes with it:
这是随附的布局代码:
<?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>
The ScrollView
and associated child elements(not shown here) is the main content of the page.
在ScrollView
和相关的子元素(这里没有显示)是页面的主要内容。
Also modified the theme to:
还将主题修改为:
<style name="EventsTheme" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
While the IllegalStateException
and RuntimeException
are gone I can't see the CoordinatorLayout
again.
虽然IllegalStateException
和RuntimeException
消失了,我再也看不到了CoordinatorLayout
。
false
错误的
回答by IntelliJ Amiya
Whats your logcat Throws
什么是你的 logcat 抛出
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
java.lang.IllegalStateException:此活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。
- All of your themes (that want an Action Bar/Toolbar) must inherit from Theme.AppCompat. There are variants available, including Light and NoActionBar.
- 您的所有主题(需要Action Bar/Toolbar)都必须从Theme.AppCompat继承。有可用的变体,包括 Light 和 NoActionBar。
Please Read Official Document about AppCompat
请阅读AppCompat官方文档
What should you do
你该怎么办
Please set
parent="Theme.AppCompat.NoActionBar"
instead of parent="Theme.AppCompat.Light"Avoid Duplicate LibraryCalling .
support:appcompat
called two times in yourbuild.gradle
section .
请设置
parent="Theme.AppCompat.NoActionBar"
而不是 parent="Theme.AppCompat.Light"避免重复库调用。
support:appcompat
在您的build.gradle
部分中调用了两次。
You can check similar types of question on SO
您可以在SO上查看类似类型的问题
回答by RexSplode
Why do you need to use Toolbar
? Do you need to support some old Android versions?
The point is that a Toolbar
is not actually ActionBar
. It lacks some functionality and forces you to use definite Themes on your Activities. So if you don't need to support some old versions of Android (v7.Toolbar is for SDK 7 (Android 2.1).
为什么需要使用Toolbar
?你需要支持一些旧的Android版本吗?关键是 aToolbar
实际上不是ActionBar
。它缺乏一些功能并迫使您在您的活动中使用明确的主题。所以如果你不需要支持一些旧版本的Android (v7.Toolbar 是针对SDK 7(Android 2.1)的)。
Oh, and by the way: what are you trying to do is called collapsing toolbar layout. Check this link for tutorial http://antonioleiva.com/collapsing-toolbar-layout/
哦,顺便说一句:您要尝试做什么称为折叠工具栏布局。检查此链接以获取教程 http://antonioleiva.com/collapsing-toolbar-layout/
P.S. Do not compile different versions of the same library! Don't do this:
PS 不要编译同一个库的不同版本!不要这样做:
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'
Instead, change it to this:
相反,将其更改为:
compile 'com.android.support:appcompat-v7:23.0.1'
compile project(':viewPagerIndicator')
compile 'com.android.support:design:23.0.1'
回答by Pavitra Kansara
OK! the famous Toolbar problem.
行!著名的工具栏问题。
As from what it seems, try making few changes in the code you have posted.
从表面上看,尝试对您发布的代码进行一些更改。
1)If you want ActionBar in other activities but this one, create a new style for NoActionBar and add that style to this Activity
1)如果你想在其他活动中而不是这个活动中使用ActionBar,请为NoActionBar创建一个新样式并将该样式添加到此活动中
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Noe in you AndroidManifest.xml
, assign this theme so that only EventsDetailsActivity
will nothave ActionBar but rest will.
野老你AndroidManifest.xml
,分配这个主题,以便只有EventsDetailsActivity
将不会有动作条,但其余的将。
<activity
android:name="EventsDetailsActivity"
android:theme="@style/AppTheme.NoActionBar">
2)The ScrollView
does not work well with the CoordinatorLayout
. So in order to achieve ‘Flexible Space with Image‘try NestedScrollView
instead of ScrollView
. This will give you proper animation effect.
2)在ScrollView
不与很好地工作CoordinatorLayout
。因此,为了实现“带有图像的灵活空间”,请尝试NestedScrollView
代替ScrollView
. 这将为您提供适当的动画效果。
3)Change the ActionBar
with a Toolbar
in your EventsDetailsActivity
.
3)改变ActionBar
了Toolbar
你EventsDetailsActivity
。
Hope it helps.
希望能帮助到你。