【发布时间】: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 用于我想使用“带图像的灵活空间”设计的特定活动并不能解决前面提到的CoordinatorLayout 和AppBarLayout 问题。
请告诉我该怎么做!我已经阅读了许多关于同一问题的 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>
虽然IllegalStateException 和RuntimeException 消失了,但我再也看不到CoordinatorLayout。
假
【问题讨论】:
-
您是否将您的支持设计库添加为库项目
-
是的,我做到了。我已经更新了我的问题。
-
尝试将布局预览更改为关于 Api-19 的较低 Api
-
更改预览并不是真正的问题。当我在手机上上传应用程序时,我在活动中看不到
CoordinatorLayout部分。 -
更新了有关按原样运行应用程序的详细信息的问题。
标签: java android material-design android-theme