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)
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