Error inflating class com.google.android.material.tabs.TabLayout
Asked Answered
A

9

8

I am trying to implement Tab Layout but unfortunately getting this error ,

i have added my xml code ,can anyone help figuring out what i am doing incorrectly.

the following is the error log i get

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vvdntech.design_material/com.example.vvdntech.design_material.LoggedIn}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class com.google.android.material.tabs.TabLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
 Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class com.google.android.material.tabs.TabLayout
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:467)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at com.example.vvdntech.design_material.LoggedIn.onCreate(LoggedIn.java:17)
    at android.app.Activity.performCreate(Activity.java:6582)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2532)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:207) 
    at android.app.ActivityThread.main(ActivityThread.java:5776) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
 Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class com.google.android.material.tabs.TabLayout
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:467) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
    at com.example.vvdntech.design_material.LoggedIn.onCreate(LoggedIn.java:17) 
    at android.app.Activity.performCreate(Activity.java:6582) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.material.tabs.TabLayout" on path: DexPathList[[zip file "/data/app/com.example.vvdntech.design_material-1/base.apk", zip file "/data/app/com.example.vvdntech.design_material-1/split_lib_dependencies_apk.apk", 

This is the xml file i have

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoggedIn">


    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/pager">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.tabs.TabItem
                android:icon="@drawable/b"
                android:text="tab1" />
            <com.google.android.material.tabs.TabItem
                android:icon="@drawable/b"
                android:text="tab2"/>
            <com.google.android.material.tabs.TabItem
                android:icon="@drawable/b"
                android:text="tab3"/>

        </com.google.android.material.tabs.TabLayout>

    </android.support.v4.view.ViewPager>

</RelativeLayout>

Main Activity i haven't added anything yet. this is totally practice , so if any small mistakes in code , do tell.

EDIT : i am also attaching my gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.vvdntech.design_material"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:design:28.0.0-alpha3'
}

& https://material.io/develop/android/components/tab-layout/ i am using this link where the tab is inside view pager.

thnx in advance

Avunculate answered 6/7, 2018 at 7:2 Comment(8)
TabLayout should be outside the viewpager and you need to connect them in java file using setupViewPager(); methodZook
Post your java class.Sypher
Check this #31713063Vaporetto
Make sure you have Design Support LibraryZook
@Sypher java class i havent added anythingAvunculate
@KushaalSingla i have the support library & according to new way it can be inside the view pager tooAvunculate
@AnkitDubey i already checked the link , nothing worksAvunculate
@AnjaniMittal Did you found the solution , i am facing the same issue .Considerable
C
8

The source of the error is that com.google.android.material.tabs.TabLayout does not come from the support libraries but rather from the material design library as @Doflaminhgo pointed out.

dependencies {
    // ...
    implementation 'com.google.android.material:material:1.0.0'
    // ...
}

Reference https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md

I am not sure why the Android reference documentation describes use of Material Components without apparently mentioning where to find it anywhere in the section.

Colchicine answered 24/8, 2018 at 8:24 Comment(1)
this is only incase u r using androidx artifacts, actually in case of the question asker, this will result in compile error because of mixing androidx and support dependenciesForestry
C
5

this is a bit late, but for anyone who might be looking into this for the future:

the package you need to use is not com.google.android.material.tabs (although it is the one used in the material design website), but it is android.support.design.widget instead (both for TabLayout and TabItem). here is an example usage:

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabItem
            android:icon="@android:drawable/ic_menu_help"
            android:text="Active"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <android.support.design.widget.TabItem
            android:icon="@android:drawable/ic_menu_help"
            android:text="History"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <android.support.design.widget.TabItem
            android:icon="@android:drawable/ic_menu_help"
            android:text="Ignored"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>

    </android.support.design.widget.TabLayout>
Crick answered 16/8, 2018 at 20:22 Comment(1)
If anyone is looking at this answer now, please note that if you are using Android then you actually need the material dependency and not the support one.Lasso
S
5

I had the same problem with Tablayout(lib Masterial), and solved it as follows

Add style.xml

<style name="ThemeCustom.TabLayout" parent="Theme.MaterialComponents.Light"/>

layout.xml

<com.google.android.material.tabs.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeCustom.TabLayout">

</com.google.android.material.tabs.TabLayout>

It solved my problem. Hope it helps others in the future.

Smiley answered 28/7, 2021 at 5:30 Comment(1)
Thanks Chanh - you saved my sorry ass. I owe you lots of beersQuintanilla
A
4

As pointed here and here, your app should use a theme that inherits Theme.MaterialComponents themes or should have desired attributes.

Appal answered 20/7, 2019 at 12:43 Comment(1)
This is kinda my issue. In my case, the other device that I was used is on dark mode, and I haven't setup the dark mode theme as my previous device isn't using that. I just copied the content of the original theme to the dark mode theme, and the error just fixedBeatification
T
4

It could be that it's looking for values which don't exist in your current app theme. Therefore, make sure your app theme starts with Theme.MaterialComponents instead of Theme.AppCompat.

Tourbillion answered 24/1, 2020 at 14:59 Comment(0)
E
0

May your xml file version is limited...

enter image description here

also see this Link

Ebro answered 6/7, 2018 at 7:13 Comment(0)
L
0

Go through your support libraries in gradle file, all support libs must have the same version, probably issue of mixed versions.

Lucinalucinda answered 6/7, 2018 at 7:23 Comment(1)
added the gradle , no mismatch in versionAvunculate
O
0

On my activity_main it was saying only tablayout but I have changed to the com.google.android.material.tabs.TabLayout and the problem got fixed

Outfox answered 6/8, 2020 at 6:46 Comment(0)
K
-1

I found the answer from the maintainer.

theme must be android:theme="@style/Theme.AppCompat.Light.NoActionBar" or its inherit

Kamal answered 21/5, 2022 at 19:18 Comment(1)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Largeminded

© 2022 - 2024 — McMap. All rights reserved.