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,
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(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.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android: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"?>
<RelativeLayout xmlns: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">
<include
android:id="@+id/this_toolbar"
layout="@layout/toolbar"/>
<TextView
android: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
public class MainActivity extends AppCompatActivity {
TextView m_toolbar_title;
@Override
protected void onCreate(@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.Toolbar
xmlns: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.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android: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
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Solution 4:
change your main activity xml to this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns: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">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
tools:layout_editor_absoluteY="45dp"
tools:layout_editor_absoluteX="0dp" />
<TextView
android: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
<activity
android:name=".ToolbarActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Design.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android: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"