Espresso InflateException NavigationView

时间:2016-02-08 13:52:10

标签: android android-espresso

我正在尝试设置一个虚拟测试来开始我的android项目。但是,我无法使用浓缩咖啡运行我的应用程序。没有测试,应用程序工作正常。

这是回溯:

Running tests
Test running started
android.view.InflateException: Binary XML file line #30: Error inflating class android.support.design.widget.NavigationView
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:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.plop.remote.MainActivity.onCreate(MainActivity.java:62)
at android.app.Activity.performCreate(Activity.java:5953)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
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:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
... 22 more
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:97)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:165)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:94)
... 25 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
... 31 more
Caused by: java.lang.IllegalStateException: Binary XML file line #17: Unable to find LayoutManager android.support.v7.widget.@2131296535
at android.support.v7.widget.RecyclerView.createLayoutManager(RecyclerView.java:506)
at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:444)
at android.support.design.internal.NavigationMenuView.<init>(NavigationMenuView.java:40)
at android.support.design.internal.NavigationMenuView.<init>(NavigationMenuView.java:36)
... 34 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.@2131296535" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.plop.remote.test-1/base.apk", zip file "/data/app/com.plop.remote-2/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:486)
... 37 more
Suppressed: java.lang.ClassNotFoundException: Invalid name: android.support.v7.widget.@2131296535
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)
... 39 more


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.plop.remote/com.plop.remote.MainActivity}: android.view.InflateException: Binary XML file line #30: Error inflating class android.support.design.widget.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
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:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class android.support.design.widget.NavigationView
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:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.plop.remote.MainActivity.onCreate(MainActivity.java:62)
at android.app.Activity.performCreate(Activity.java:5953)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
... 10 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
... 22 more
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:97)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:165)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:94)
... 25 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
... 31 more
Caused by: java.lang.IllegalStateException: Binary XML file line #17: Unable to find LayoutManager android.support.v7.widget.@2131296535
at android.support.v7.widget.RecyclerView.createLayoutManager(RecyclerView.java:506)
at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:444)
at android.support.design.internal.NavigationMenuView.<init>(NavigationMenuView.java:40)
at android.support.design.internal.NavigationMenuView.<init>(NavigationMenuView.java:36)
... 34 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.@2131296535" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.plop.remote.test-1/base.apk", zip file "/data/app/com.plop.remote-2/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:486)
... 37 more
Suppressed: java.lang.ClassNotFoundException: Invalid name: android.support.v7.widget.@2131296535
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)
... 39 more

Test running failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

我的build.gradle:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.plop.remote"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile project(':Box')
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'me.neavo:volley:2014.12.09'
    compile 'com.google.code.gson:gson:2.3.1'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.1') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test:runner:0.4.1') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude module: 'support-v4'
    }
}

和看似问题的XML

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.plop.remote.MainActivity">


    <com.plop.remote.NonSwipeableViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="50dp">

    </com.plop.remote.NonSwipeableViewPager>

    <SurfaceView
        android:layout_width="0px"
        android:layout_height="0px"
        android:visibility="gone" />

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/menuBackground"
        app:headerLayout="@layout/nav_header_main"
        app:itemTextColor="@color/textColor"
        app:menu="@menu/menu_main_drawer" />

</android.support.v4.widget.DrawerLayout>

到目前为止,我已经在gradle文件中尝试了很多东西而没有成功。比如我觉得:

Why would adding espresso-contrib cause an InflateException?

与我相似,除了使用该gradle文件,我的应用程序在没有测试的情况下仍能正常运行。

2 个答案:

答案 0 :(得分:2)

同样的问题并用此解决:

androidTestCompile ("com.android.support.test.espresso:espresso-core:$espressoVersion"){
    exclude group: 'com.android.support', module: 'appcompat-v7'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude module: 'recyclerview-v7'  
}
androidTestCompile ("com.android.support.test.espresso:espresso-contrib:$espressoVersion"){
    exclude group: 'com.android.support', module: 'appcompat-v7'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'design'
    exclude module: 'recyclerview-v7'
}

答案 1 :(得分:0)

检查以下配置以检查您是否遗漏了某些内容

android {
    dataBinding {
        enabled = true
    }
    compileSdkVersion 23
    buildToolsVersion '23.0.2'

    defaultConfig {
        applicationId "com.piotr.example"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    androidTestCompile "com.android.support:support-annotations:$ASVersion"
    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: 'recyclerview-v7'
    }
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile "com.android.support:appcompat-v7:$ASVersion"
    compile "com.android.support:support-v4:$ASVersion"
    compile "com.android.support:design:$ASVersion"
}

由于Gradle未返回任何测试,这可能意味着您忘记在Gradle配置中包含testInstrumentationRunner

希望有所帮助