Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SlidengAppAnnelLayout与Robolectric冲突_Android_Robolectric - Fatal编程技术网

Android SlidengAppAnnelLayout与Robolectric冲突

Android SlidengAppAnnelLayout与Robolectric冲突,android,robolectric,Android,Robolectric,我有一个使用SlidengUppanellayout的项目,但我需要开始使用Robolectric编写自动测试。当我将Robolectric引入该项目时,我基于浓缩咖啡的测试未能启动。当系统尝试放大SlidengAppAnnelLayout时,我会在预验证类中得到一个java.lang.IllegalAccessError:Class ref,该类被解析为意外实现 java.lang.RuntimeException: Unable to start activity ComponentInf

我有一个使用SlidengUppanellayout的项目,但我需要开始使用Robolectric编写自动测试。当我将Robolectric引入该项目时,我基于浓缩咖啡的测试未能启动。当系统尝试放大SlidengAppAnnelLayout时,我会在预验证类中得到一个java.lang.IllegalAccessError:Class ref,该类被解析为意外实现

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marcthomas.testrobolectric/com.marcthomas.testrobolectric.MyActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.sothree.slidinguppanel.SlidingUpPanelLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.sothree.slidinguppanel.SlidingUpPanelLayout
    at android.view.LayoutInflater.createView(LayoutInflater.java:620)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
    at android.app.Activity.setContentView(Activity.java:1929)
    at com.marcthomas.testrobolectric.MyActivity.onCreate(MyActivity.java:14)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.callActivityOnCreate(GoogleInstrumentation.java:428)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
    ... 11 more
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at android.view.LayoutInflater.createView(LayoutInflater.java:594)
    ... 23 more
 Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
    at com.sothree.slidinguppanel.ViewDragHelper.<init>(ViewDragHelper.java:392)
    at com.sothree.slidinguppanel.ViewDragHelper.create(ViewDragHelper.java:348)
    at com.sothree.slidinguppanel.ViewDragHelper.create(ViewDragHelper.java:361)
    at com.sothree.slidinguppanel.SlidingUpPanelLayout.<init>(SlidingUpPanelLayout.java:334)
    at com.sothree.slidinguppanel.SlidingUpPanelLayout.<init>(SlidingUpPanelLayout.java:263)
    ... 26 more
如果我从项目中的activity_my.xml布局文件中删除slidenguppannellayout,测试将再次成功运行,因此这似乎与这两个组件有关


我在github上创建了一个演示此行为的演示项目。克隆代码并运行./gradlew connectedAndroidTest将重现此问题。

要解决此问题,我需要添加

exclude module: 'support-v4'
到应用程序模块gradle文件中的排除列表,其中声明了androidTestCompile for robolectric。见下面的解释

此错误可归结为Gradle配置错误。原因是在构建中多次声明了依赖项,需要排除其中一个依赖项

要解决这个问题,请查看异常的最后一部分

ViewDragHelper是罪魁祸首,因此如果在IDE Android Studio中,在OSX中打开SearchforClass name命令-O并输入ViewDragHelper。您将看到返回了多个jar。你需要排除你不想要的那个

在这种情况下,我们有android-19 sdk、support-v4-19和support-v4-20以及library-2.0.1 slideuppanellayout库来解决这个问题,我需要排除support-v4

因此,在包含robolectric的项目的构建gradle文件中,我需要将以下内容添加到排除列表中

排除模块:“support-v4”