[英]java.lang.ClassCastException: android.support.design.widget.AppBarLayout cannot be cast to android.support.v7.widget.Toolbar
I have no idea why i am getting this exception. 我不知道为什么我要得到这个例外。
Here is my code - 这是我的代码-
toolbar.xml 工具栏
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
I am including this layout in my xml file - 我将此布局包含在我的xml文件中-
<include android:id="@+id/photoGalleryToolbar"
layout="@layout/toolbar"/>
And then i am using following code in my activity - 然后我在活动中使用以下代码-
Toolbar toolbar = (Toolbar) findViewById(R.id.photoGalleryToolbar);
setSupportActionBar(toolbar);
final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_keyboard_backspace_white_24dp);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle("Kidster Photo Gallery");
actionBar.setDisplayHomeAsUpEnabled(true);
}
The exception is coming at following line - 例外情况出现在以下行-
Toolbar toolbar = (Toolbar) findViewById(R.id.photoGalleryToolbar);
I had checked the imports and they all are right and i am using support library. 我已经检查了导入,它们都正确,并且我正在使用支持库。
Replace you code : 替换您的代码:
Toolbar toolbar = (Toolbar) findViewById(R.id.photoGalleryToolbar);
with: 与:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
You made a mistake on the id, R.id.photoGalleryToolbar
is the root view of the layout which you include here is the AppBarLayout
. 您在id上弄错了,
R.id.photoGalleryToolbar
是布局的根视图,其中包括AppBarLayout
。 The Toolbar
's id is R.id.toolbar
you defined in the toolbar.xml
. 该
Toolbar
的ID是R.id.toolbar
您在定义toolbar.xml
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.