Gnana Prakash
Gnana Prakash

Reputation: 101

Error in Android Toolbar

I am using toolbar in android which works fine in lollipop version but unfortunately it doesn't works in older versions.

Toolbar xml:

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

Java class:

public class MainAct extends AppCompatActivity{
Toolbar mtoolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.act_main);
    mtoolbar=(Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(mtoolbar);

}

Styles:

<resources>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

Gradle:

    apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
    applicationId "nidhinkumar.reccs"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}  
  }dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:24.0.0-alpha1'
    }

I have tried changing the style to No action bar but still the app doesn't runs on the older device and i am getting the following error.

java.lang.RuntimeException: Unable to start activity ComponentInfo{nidhinkumar.reccs/nidhinkumar.reccs.MainAct}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:130)
        at nidhinkumar.reccs.MainAct.onCreate(MainAct.java:15)
        at android.app.Activity.performCreate(Activity.java:5286)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)

Upvotes: 0

Views: 1561

Answers (4)

Ashwini
Ashwini

Reputation: 245

compile 'com.android.support:design:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
  • These libraries offer backward-compatible versions of new features, provide useful UI elements that are not included in the framework. So for devices below Lollipop these features are not included. So these support libraries makes things possible for versions below Lollipop.

Upvotes: 0

Franklyn
Franklyn

Reputation: 325

Try this not so sure may be because of your compileSdkVersion is higher,
change compile 'com.android.support:appcompat-v7:24.0.0-alpha1' to

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:design:23.1.1'

if this does not work, change your build.gradle(Project) classPath 
to a lower varsion(say 1.3.0 or 1.3.1)

Upvotes: 2

Vivek_Neel
Vivek_Neel

Reputation: 1353

Change this :

android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"

To :

android:minHeight="@dimen/actionBarSize"
android:background="@color/colorPrimary"

Upvotes: 0

Damini Mehra
Damini Mehra

Reputation: 3347

Add this code in your xml file:

<include
android:id="@+id/tool_bar"
layout="@layout/actionbar_main"></include>

Upvotes: 0

Related Questions