1

I am trying to use TabLayout in my AS project. When I run the app on the emulator the app collapse immediately. If I remove the TabLayout view the app runs appropiriately.

I am using androidx.

As in the answers to this question, I tried to add

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:your_api_version_number.0.0'
    compile 'com.android.support:design:+'

to the dependencies and I also tried to add

implementation 'com.android.support:support-v4:22.2.0'
implementation 'com.android.support:design:22.2.0'

to the dependencies. Both solutions did not help me.

How can I use TabLayout appropriately?

Here's the code:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_and_vertical_margin"
    android:paddingTop="@dimen/activity_horizontal_and_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_and_vertical_margin"
    android:paddingBottom="@dimen/activity_horizontal_and_vertical_margin"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/toolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

</RelativeLayout>

Here's build.gradle (Module: app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "android.example.tabexperiment"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:29.0.2'
    compile 'com.android.support:design:+'

}

EDIT:

Here's the Logcat output:

2019-10-11 13:13:32.022 8427-8427/? I/zygote: Not late-enabling -Xcheck:jni (already on)
2019-10-11 13:13:32.047 8427-8427/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
2019-10-11 13:13:32.443 8427-8427/android.example.tabexperiment I/zygote: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
2019-10-11 13:13:32.443 8427-8427/android.example.tabexperiment I/zygote:     at void androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, androidx.core.view.OnApplyWindowInsetsListener) (ViewCompat.java:2421)

at........at

2019-10-11 13:13:32.474 8427-8427/android.example.tabexperiment I/zygote:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:767)
2019-10-11 13:13:32.619 8427-8427/android.example.tabexperiment D/AndroidRuntime: Shutting down VM


    --------- beginning of crash
2019-10-11 13:13:32.666 8427-8427/android.example.tabexperiment E/AndroidRuntime: FATAL EXCEPTION: main
    Process: android.example.tabexperiment, PID: 8427
    java.lang.RuntimeException: Unable to start activity ComponentInfo{android.example.tabexperiment/android.example.tabexperiment.MainActivity}: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class android.support.design.widget.TabLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class android.support.design.widget.TabLayout
     Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class android.support.design.widget.TabLayout
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.TabLayout" on path: DexPathList[[zip file "/data/app/android.example.tabexperiment-RjSmL-WuWCmaiNehU5C96w==/base.apk"],nativeLibraryDirectories=[/data/app/android.example.tabexperiment-RjSmL-WuWCmaiNehU5C96w==/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:606)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        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 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at android.example.tabexperiment.MainActivity.onCreate(MainActivity.java:14)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

J. Doe
  • 299
  • 3
  • 12
  • Did you add this compile 'com.android.support:appcompat-v7:your_api_version_number.0.0' or actually replaced it with the proper version number? If the former, there's your problem right there. Essentially what I'm saying is you should replace the words your_api_version_number with your current API version number – Nikos Hidalgo Oct 08 '19 at 16:18
  • I added it. What shoud I replace it with? (I can't paste `build.gradle` (Module: app) because it is too long for a comment). – J. Doe Oct 08 '19 at 16:22
  • in your gradle check what android version you're targeting. In the dependency mentioned above replace the words your_api_version_number with that said number. – Nikos Hidalgo Oct 08 '19 at 16:35
  • I added to the question the `build.gradle`(Module: app) file.When I add`compile fileTree(dir: 'libs', include: ['*.jar'])`,`compile 'com.android.support:appcompat-v7:29.0.0'`,`compile 'com.android.support:design:+'` or `compile fileTree(dir: 'libs', include: ['*.jar'])`,`compile 'com.android.support:appcompat-v7:29.0.2'`,`compile 'com.android.support:design:+'` the problem remains.Also, when I add this then there's a warning on the3`compile`s says "'compile' is deprecated.replace with'implementation'".And whed I replace them the problem also remains. – J. Doe Oct 08 '19 at 16:38

1 Answers1

1

You're targeting android 10, hence the support library's not working. There is no version 29 of the support library since it was included in the androidX packages. you can remove the references to the support library and replace them with the following dependency in order to use the TabLayout:

remove this:

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:29.0.2'
compile 'com.android.support:design:+'

replace it with this:

implementation 'com.google.android.material:material:1.0.0'

EDIT (after logcat error): Please note that once you start using the material dependency and AndroidX, you need to be declaring your TabLayout like this: com.google.android.material.tabs.TabLayout and not like this: android.support.design.widget.TabLayout

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
  • Actually it didn't solve the problem. The reactio nremains the same. – J. Doe Oct 11 '19 at 08:52
  • @J.Doe can you add your logcat with the crash error please? Even though it was necessary to fix your dependencies, there might be other issues as well. – Nikos Hidalgo Oct 11 '19 at 09:33
  • The error is `Unable to start activity ComponentInfo{android.example.tabexperiment/android.example.tabexperiment.MainActivity}: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class android.support.design.widget.TabLayout`. I searched for this error and the solution was to add a theme to the `TabLayout` view, but as you can see in the code I posted in the question, there's a theme in the `TabLayout` view: `android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"`. – J. Doe Oct 11 '19 at 09:49
  • @J.Doe inflation errors can happen for different reasons. Sometimes after an inflation error you can get more specific logs. If you were to look further down the list underneath the lines which you posted above, is there a mention of a more specific cause for the inflation error? – Nikos Hidalgo Oct 11 '19 at 10:04
  • @J.Doe Can you try declaring your TabLayout as com.google.android.material.tabs.TabLayout in your xml file, if you're not doing this already. – Nikos Hidalgo Oct 11 '19 at 10:32
  • Well, it solved the problem. I declared `com.google.android.material.tabs.TabLayout` as you suggested instead of `android.support.design.widget.TabLayout` as I declared in the first place. Thanks you! – J. Doe Oct 11 '19 at 12:10
  • @J.Doe glad we got to the bottom of this! – Nikos Hidalgo Oct 11 '19 at 13:04