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

匿名 (未验证) 提交于 2019-12-03 02:48:02

问题:

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!

回答1:

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.



回答2:

android.support.v7.widget.Toolbar toolbar; 

toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!