我正在尝试在我的AS项目中使用TabLayout
。当我在模拟器上运行该应用程序时,该应用程序立即崩溃。如果我删除了TabLayout
视图,则该应用会正确运行。
我正在使用androidx。
在对this问题的回答中,我尝试添加
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:your_api_version_number.0.0'
compile 'com.android.support:design:+'
依赖项,我也尝试添加
implementation 'com.android.support:support-v4:22.2.0'
implementation 'com.android.support:design:22.2.0'
到依赖项。两种解决方案都对我没有帮助。
如何正确使用TabLayout
?
代码如下:
<?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>
这里是build.gradle
(模块:应用):
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:+'
}
编辑:
这是Logcat的输出:
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)
答案 0 :(得分:1)
您的目标是android 10,因此支持库不起作用。由于androidX软件包中包含支持库,因此没有版本29。您可以删除对支持库的引用,并使用以下依赖项替换它们,以便使用TabLayout:
删除此:
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:29.0.2'
compile 'com.android.support:design:+'
替换为:
implementation 'com.google.android.material:material:1.0.0'
编辑(在出现logcat错误之后):请注意,一旦开始使用材质依赖项和AndroidX,则需要这样声明您的TabLayout: com.google.android.material.tabs.TabLayout
而不是这样的:android.support.design.widget.TabLayout