Error inflating class android.support.v7.widget.Toolbar

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

I have moved my app from using actionbar to toolbar however end up with the error (full error at bottom). My code:

My Activity:

public class MapsActivity extends ActionBarActivity implements LocationListener {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_maps);         ...         mDrawerLayout.setDrawerListener(mDrawerToggle);          toolbar = (Toolbar) findViewById(R.id.toolbar);      if (toolbar != null)     {         setSupportActionBar(toolbar);         getSupportActionBar().setDisplayHomeAsUpEnabled(true);         getSupportActionBar().setHomeButtonEnabled(true);         getSupportActionBar().setElevation(0); // or other     }          ...     } ... } 

v21/style.xml

    <?xml version="1.0" encoding="utf-8"?> <resources>     <!-- your app's theme inherits from the Material theme -->     <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">         <item name="windowActionBar">false</item>         <item name="android:windowNoTitle">true</item>         <!-- Main theme colors -->         <!--   your app branding color for the app bar -->         <item name="android:colorPrimary">@color/primary</item>         <!--   darker variant for the status bar and contextual app bars -->         <item name="android:colorPrimaryDark">@color/primary_dark</item>         <item name="android:navigationBarColor">@color/primary</item>          <!--   theme UI controls like checkboxes and text fields -->         <item name="android:colorAccent">@color/accent</item>     </style> </resources> 

style.xml

<resources>  <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">     <item name="windowActionBar">false</item>     <item name="android:windowNoTitle">true</item> </style>  </resources> 

gradle dependency

dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     compile 'com.google.android.gms:play-services:6.5.87'     compile 'com.android.support:appcompat-v7:21.0.3' } 

my XML layout (after changes made by answer)

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >  <android.support.v7.widget.Toolbar     android:id="@+id/toolbar"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:minHeight="?attr/actionBarSize"     android:background="?attr/colorPrimary"     />  <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The main content view -->     <RelativeLayout         android:layout_width="match_parent"         android:layout_height="match_parent">          </RelativeLayout>  <!-- The navigation drawer --> <RelativeLayout     android:id="@+id/left_drawer"     android:layout_width="240dp"     android:layout_height="match_parent"     android:layout_gravity="start"     android:background="#111">s       </RelativeLayout>  </android.support.v4.widget.DrawerLayout> </LinearLayout> 

Error below answer gives:

回答1:

replace your parent theme to @style/Theme.AppCompat.NoActionBar

<style name="AppTheme" parent="@style/Theme.AppCompat.NoActionBar">    <item name="windowActionBar">false</item>    <item name="android:windowNoTitle">true</item> </style> 


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