Skip to content Skip to sidebar Skip to footer

Inflateexception When Adding Toolbar In Android

I am new to material design and i am trying a few things out. For my app minimum sdk is 21. But my app crashes with the following code. code for toolbar is

Solution 1:

Your toolbar id is toolbar not tool_bar. Use like,

publicclassMainActivityextendsAppCompatActivity  {
Toolbar toolbar;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar=(Toolbar) findViewById(R.id.toolbar);

  }
}

EDIT 1 : Example to use Toolbar

toolbar.xml

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbarandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/colorPrimary"xmlns:android="http://schemas.android.com/apk/res/android"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:text="@string/app_name"android:textColor="#fff"android:gravity="center"android:textStyle="bold"android:textSize="18dp"/></android.support.v7.widget.Toolbar>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:ads="http://schemas.android.com/apk/res-auto"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><includeandroid:id="@+id/this_toolbar"layout="@layout/toolbar"/><TextViewandroid:id="@+id/toolbar_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/this_toolbar"android:text="TextView"/></RelativeLayout>

MainActivity.java

publicclassMainActivityextendsAppCompatActivity {

TextView m_toolbar_title;

@OverrideprotectedvoidonCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    m_toolbar_title = (TextView)findViewById(R.id.toolbar_title);
    m_toolbar_title.setText("Activity Title");

    }

}

Your layout="@layout/toolbar" in activity_main.xml, toolbar should be file_name which is having <Toolbar>

Have a look. This may helps you.

Solution 2:

Try this.

Your toolbar.xml should be like this .

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbarxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/tool_bar"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="?attr/colorPrimary"app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    ></android.support.v7.widget.Toolbar>

Solution 3:

try this code

<android.support.design.widget.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/AppTheme.AppBarOverlay"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:popupTheme="@style/AppTheme.PopupOverlay" /></android.support.design.widget.AppBarLayout>

and add these lines in style.xml

<stylename="AppTheme.AppBarOverlay"parent="ThemeOverlay.AppCompat.Dark.ActionBar" /><stylename="AppTheme.PopupOverlay"parent="ThemeOverlay.AppCompat.Light" />

Solution 4:

change your main activity xml to this

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.sagar.moviesuccesspredictor.MainActivity"><includeandroid:id="@+id/tool_bar"layout="@layout/tool_bar"tools:layout_editor_absoluteY="45dp"tools:layout_editor_absoluteX="0dp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"android:layout_below="@+id/tool_bar"android:layout_marginTop="10dp"/></RelativeLayout>

the problem was in tag instead of android:layout , you have to use layout:yourLayoutfile. and make sure you use support toolbar in mainActivity as well

Solution 5:

Open logcat, it may suggest that Toolbar conflicts with existent ActionBar. Solution is modify AndroidManifest.xml

<activityandroid:name=".ToolbarActivity"android:label="@string/app_name"android:theme="@style/Theme.Design.Light.NoActionBar"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity>

Add theme which has no default ActionBar.

Post a Comment for "Inflateexception When Adding Toolbar In Android"