为什么添加espresso-contrib会导致InflateException?

时间:2015-06-01 16:10:49

标签: android android-recyclerview android-espresso

在我的build.gradle文件中,我有支持库依赖项:

compile "com.android.support:appcompat-v7:22.2.0"
compile "com.android.support:recyclerview-v7:22.2.0"
compile "com.android.support:design:22.2.0"

我也有espresso测试的依赖项:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'

此时所有内容都运行良好,但当我添加espresso-contrib的依赖项时,我在InflateException上获得RecyclerView

android.view.InflateException: Binary XML file line #33: Error inflating class android.support.v7.widget.RecyclerView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
...
Caused by: java.lang.IllegalStateException: Binary XML file line #33: Unable to find LayoutManager android.support.v7.widget.@2131296518
at android.support.v7.widget.RecyclerView.createLayoutManager(RecyclerView.java:500)
at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:438)
at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:404)
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.@2131296518" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.myapp.debug.test-1/base.apk", zip file "/data/app/com.myapp.debug-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.support.v7.widget.RecyclerView.createLayoutManager(RecyclerView.java:480)
...
Suppressed: java.lang.ClassNotFoundException: Invalid name: android.support.v7.widget.@2131296518
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)

有关为什么会发生这种情况以及如何解决这个问题的想法?

3 个答案:

答案 0 :(得分:51)

在build.gradle中试试这个:

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2'){
    exclude group: 'com.android.support', module: 'appcompat-v7'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude module: 'recyclerview-v7'
}

答案 1 :(得分:7)

我有关于错误膨胀类回收器视图的相同问题,并尝试了多次使用各种代码,最后我通过在项目gradle中添加这些代码解决了这个问题:

 androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
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'
}

其次,您必须确保在最新版本中使用卡片视图和回收者视图:

compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'

然后,您可以在其活动布局中运行具有回收站视图的测试。它会正常工作,不会再出现错误。

答案 2 :(得分:3)

试试这个:

// Testing dependencies
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-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'support-annotations'
        exclude module: 'recyclerview-v7'
    }