简体   繁体   English

java.lang.ClassCastException:android.support.design.widget.AppBarLayout无法转换为android.support.v7.widget.Toolbar

[英]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.

相关问题 java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.support.v7.widget.Toolbar - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v7.widget.Toolbar java.lang.ClassCastException:android.support.v7.widget.AppCompatButton无法转换为android.support.design.widget.FloatingActionButton - java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.support.design.widget.FloatingActionButton RelativeLayout无法强制转换为android.support.v7.widget.Toolbar - RelativeLayout cannot be cast to android.support.v7.widget.Toolbar CoordinatorLayout 无法转换为 android.support.v7.widget.Toolbar - CoordinatorLayout cannot be cast to android.support.v7.widget.Toolbar ClassCastException:java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.support.v4.widget.DrawerLayout - ClassCastException : java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.v7.widget.RecyclerView$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.v7.widget.RecyclerView $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException:android.support.constraint.ConstraintLayout无法转换为android.widget.TextView - java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView 错误 - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout 无法转换为 androidx.appcompat.widget.Toolbar - Error - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout cannot be cast to androidx.appcompat.widget.Toolbar 使用android.support.v7.widget.Toolbar的NullPointerException - NullPointerException with android.support.v7.widget.Toolbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM