AndroidDev21921
AndroidDev21921

Reputation: 695

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

I'ved looked at a ton of SO questions and tried implementing them. Can't seem to figure this one one, it was working yesterday, but not anymore.

gradle:

android {
compileSdkVersion 23
buildToolsVersion "23.0.0 "

dexOptions {
    javaMaxHeapSize "4g"
}

defaultConfig {
    minSdkVersion 16
    multiDexEnabled true
    targetSdkVersion 23
}
dependencies {
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'com.android.support:design:23+'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'com.android.support:support-v4:24.0.0-alpha2'
compile 'com.github.fernandodev.easyratingdialog:easyratingdialog:1.1.0'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.parse:parse-android:1.+'
compile 'com.squareup:otto:1.3.8'
compile 'com.moengage:moe-android-sdk:6.0.10'
compile 'com.amazonaws:aws-android-sdk-mobileanalytics:2.2.9'


compile 'org.apmem.tools:layouts:1.9@aar'

compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.crashlytics.sdk.android:answers:1.3.6@aar') {
    transitive = true;
}
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
    transitive = true;
}
}

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>

android:background="@color/colorPrimary"
android:elevation="4dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="@dimen/abc_action_bar_default_height_material"
android:theme="@style/ThemeOverlay.AppCompat.Dark"/>

BaseActivity.class

public abstract class BaseActivity extends AppCompatActivity {
private MoEHelper mHelper;
public static MobileAnalyticsManager analytics;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mHelper = new MoEHelper(this);


    try {
        analytics = MobileAnalyticsManager.getOrCreateInstance(
                this.getApplicationContext(),
                "", //Amazon Mobile Analytics App ID
                "" //Amazon Cognito Identity Pool ID
        );
    } catch(InitializationException ex) {
        Log.e(this.getClass().getName(), "Failed to initialize Amazon Mobile Analytics", ex);
    }



}

@Override
protected void onStart() {
    super.onStart();
    mHelper.onStart(this);
}

@Override
protected void onStop() {
    super.onStop();
    mHelper.onStop(this);


}

@Override
protected void onPause() {
    super.onPause();
    mHelper.onPause(this);
    if(analytics != null) {
        analytics.getSessionClient().pauseSession();
        //Attempt to send any events that have been recorded to the Mobile Analytics service.
        analytics.getEventClient().submitEvents();
    }
}

@Override
protected void onResume() {
    super.onResume();
    mHelper.onResume(this);
    if(analytics != null)  {
        analytics.getSessionClient().resumeSession();
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mHelper.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mHelper.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    mHelper.onNewIntent(this, intent);
}
}

Activity class

public abstract class Activity extends BaseActivity
{
protected Toolbar toolbar;

// region Activity Lifecycle

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(getContentView());

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null)
    {
        setSupportActionBar(toolbar);
    }
}

// endregion

// region Getters/Setters

protected String getActionBarTitle()
{
    String actionBarTitle = "";

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null && actionBar.getTitle() != null)
    {
        actionBarTitle = actionBar.getTitle().toString();
    }

    return actionBarTitle;
}

protected void setActionBarTitle(int resourceId)
{
    String actionBarTitle = getString(resourceId);
    setActionBarTitle(actionBarTitle);
}

protected void setActionBarTitle(String actionBarTitle)
{
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null)
    {
        if (!StringUtilities.isNullOrEmpty(actionBarTitle))
        {

            SpannableString spannableString = new SpannableString(actionBarTitle);
            spannableString.setSpan(new TypefaceSpan("sans-serif-light"), 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            actionBar.setTitle(spannableString);
            actionBar.setDisplayShowTitleEnabled(true);
        }
        else
        {
            actionBar.setDisplayShowTitleEnabled(false);
        }
    }
}

// endregion

abstract protected int getContentView();
}

Activity_main.xml

Logcat

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:640)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:904)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.app.Activity.onCreate(Activity.java:24)
        at com.app.DrawerActivity.onCreate(DrawerActivity.java:51)
        at com.app.MainActivity.onCreate(MainActivity.java:86)
        at android.app.Activity.performCreate(Activity.java:6221)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.access$900(ActivityThread.java:172)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5835)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
        at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

Upvotes: 1

Views: 6471

Answers (3)

AndroidDev21921
AndroidDev21921

Reputation: 695

This is what worked specifically for me. My problem had nothing to do with themes but rather the right compile versions of the dependencies and targetSdkVersion. Updated the gradle file below. Changed the

Original Gradle

compile 'com.android.support:support-v4:24.0.0-alpha2'

Changed:

compile 'com.android.support:support-v4:23.0.0'

Essentially the issue was my this version of the library did not match with the others and the minsdkversion, so something didn't mesh well. Once i updated all the dependencies (specifically from google design library) to match each other, it worked.

Upvotes: 1

Haroon
Haroon

Reputation: 505

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

Try with this theme snip

Upvotes: 0

Francesco verheye
Francesco verheye

Reputation: 1574

I had a similar issue in the past. The android:theme was the issue, on the Toolbar or on the AppBarLayout.

Edit: code example

<android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

Option2:

<style name="MyToolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:background">@color/colorPrimary</item>
</style>

Upvotes: 0

Related Questions