IncompatibleClassChangeError:android.support.design.internal.NavigationMenuView

时间:2015-11-06 12:08:31

标签: android android-espresso

在测试使用设计库的活动时,我遇到一个非常奇怪的错误:

android.view.InflateException: Binary XML file line #28: Error inflating class android.support.design.widget.NavigationView
    at android.view.LayoutInflater.createView(LayoutInflater.java:640)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
    at de.stephanlindauer.criticalmaps.Main.onCreate(Main.java:77)
    at android.app.Activity.performCreate(Activity.java:6550)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
    at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
    at android.app.ActivityThread.access$1000(ActivityThread.java:197)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6872)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.IncompatibleClassChangeError: android.support.design.internal.NavigationMenuView

任何人都遇到了问题并找到了解决办法吗?该活动在espresso-tests之外工作

使用espresso 2.2.1

这里布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/abc_action_bar_default_height_material"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize" />

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/drawer_navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>

依赖项:

dependencies {
    androidTestCompile 'com.android.support:support-v4:23.1.0'
    androidTestCompile 'com.android.support:support-annotations:23.1.0'

    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'

    androidTestCompile 'com.squareup.spoon:spoon-client:1.2.0'
    androidTestCompile 'com.squareup.assertj:assertj-android:1.1.1'

    androidTestCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.squareup:otto:1.3.8'
    compile 'org.osmdroid:osmdroid-android:4.3'
    compile 'org.slf4j:slf4j-simple:1.6.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:support-annotations:23.1.0'
    compile 'com.android.support:design:23.1.0'
    compile 'com.squareup.okhttp:okhttp:2.5.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'org.ligi:AXT:0.35'
    compile 'com.google.dagger:dagger:2.0.2'

    provided 'javax.annotation:jsr250-api:1.0'
    apt 'com.google.dagger:dagger-compiler:2.0.2'

    testCompile 'junit:junit:4.12'
    testCompile 'com.squareup.assertj:assertj-android:1.1.0'
    testCompile 'com.android.support:support-annotations:23.1.0'
    testCompile 'org.mockito:mockito-core:1.9.5'
}

2 个答案:

答案 0 :(得分:4)

Espresso依赖于许多支持依赖项,并且从最新版本的Espresso(2.2.1)开始,它们都已过时。您已经解决了这个问题,明确地将一些作为测试依赖项包含在内,但是您忘了一个:recyclerview-v7。

您需要将recylerview-v7作为测试依赖项包含在内,或者将其从contrib中排除。我更喜欢只是排除这些依赖项,因此它将默认为我的正常编译依赖项:

androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.1') {
    exclude module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1') {
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'recyclerview-v7'
}

答案 1 :(得分:0)

尝试使用此代码替换。希望它可以正常工作。

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    app:headerLayout="@layout/layout_drawer_header"
    app:menu="@menu/drawer_menu"
    app:itemTextColor="@color/some_color"/>