My Android Studio's app collapses when I use TabLayout, how can I fix it?

J. Doe

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)

Nikos Hidalgo

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I fix this? My app works fine if I use expo app to emulate it on android, but it crashes when I build the apk

How to download my android app to my computer so I can fix it in Android Studio

How can I fix the layout views in my android app?

My published app on google play shows an android file when users download it. How can i fix this?

How can I use recycler viewer with a fragment on a tablayout (android studio Kotlin)?

How can I sign my Bundle App in android studio for AppGallery?

How can I use Android native icons in my new app?

How can I check my Android app's network consumption?

Can I still use JKS keystore to publish app to Google Play when I use Android Studio 4.0?

How i can connect my real app an android studio so i can run espresso test on

i use Glide for my image display in android studio but how can i implement Glide to my listview?

can i use my flutter codes that i wrote in android studio or visual studio for ios?and how?

My app is crashing in android studio, and I cannot figure out what's wrong when looking at my logs

My android app crashes when I use it, and Android studio shows "FATAL EXCEPTION"

My android studio project is not giving me the option to build apk. How can I fix it?

How can i merge my "Fix branch" back to "Master branch" from Android Studio?

How can i change my Project quick fix shortcut in android studio?

When i click on any link on android studio app then app is closed,how can i solve it?

How can I profile my Android app?

how can I run my android app?

How can I keep my app's notification displayed when my app is updated on Google Play store?

Please how can I fix this error in my react app

How I fix NOTHING TO SHOW in my vector asset in android studio

How can I disable click on TabLayout in Android

How can I fix my program's infinite loop when asking for new values?

How can I use universal links when I call openURL inside my own app?

How can I use keychain when I close my ios app?

When running my App from Android Studio can I force the emulator to the top?

How can I use sed and grep to fix my file

TOP Ranking

HotTag

Archive