Search code examples
androidandroid-layout

android.widget.Toolbar cannot be converted to android.support.v7.widget.Toolbar


I have a toolbar.xml:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_primary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />

I have an include tag to another of my activity layouts:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme">

<include
    layout="@layout/toolbar"
    android:id="@+id/tb">
</include>

<android.support.v7.widget.RecyclerView
    android:id="@+id/grade_list"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" />

And I have it implemented in my activity which extends AppCompatActivity:

Toolbar toolbar;
    toolbar = (Toolbar)findViewById(R.id.tb);
    setSupportActionBar(toolbar);

However, I am getting the following error when I run my app:

Error:(26, 29) error: incompatible types: android.widget.Toolbar cannot be converted to android.support.v7.widget.Toolbar

Furthermore, when I view my layout in the editor, I receive the following message:

Missing styles. Is the correct theme chosen for this layout?
Use the theme combo box above the layout to choose a different layout, or to fix the theme style references.
Failed to find style 'toolbarStyle' in current layout.

And my styles.xml:

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/color_primary</item>
    <item name="colorPrimaryDark">@color/color_primaryDark</item>
    <item name="colorAccent">@color/color_accent</item>
    <item name="colorControlNormal">@color/color_accent</item>
</style>

I don't see where I am going wrong. The correct theme is selected in the editor and my manifest file. All help is appreciated as always!


Solution

  • You use android.support.v7.widget.Toolbar in your XML, but in your java code you are importing android.widget.Toolbar which is a different type. Change your import to android.support.v7.widget.Toolbar and it should work.