FragmentActivity不能通过ActivityInstrumentationTestCase2测试 [英] FragmentActivity can not be tested via ActivityInstrumentationTestCase2

查看:205
本文介绍了FragmentActivity不能通过ActivityInstrumentationTestCase2测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对执行,利用最近发布的片段支持API的Andr​​oid应用程序的Andr​​oid单元测试的一个问题。当测试运行针对FragmentActivity以下错误显示在日志中的类加载失败。当对同一类中运行,但有来源于活性的测试工作正常。这两个类正常工作的应用程序!这意味着当简单地调用它们都正确地显示它们的布局和功能。支持罐子是构建路径的一部分,包含在项目中。

我的问题是,只有这样,才能利用碎片(和支持pre3.0机器人)是利用FragmentActivity,但如果不包括自动测试,然后有什么好处这个库。

  4-05 18:00:11.276:WARN / dalvikvm(1095):一类以意想不到的DEX解决:LCOM /例子/安卓/ APP / FragmentLayoutSupport;(0x406351a0):0x12e5c8 REF [Landroid /支持/ V4 / APP / FragmentActivity] Landroid /支持/ V4 / APP / FragmentActivity;(0x406351a0):0x12e440
04-05 18:00:11.276:WARN / dalvikvm(1095):(LCOM /例子/安卓/ APP / FragmentLayoutSupport;曾用不同的Landroid /支持/ V4 / APP / FragmentActivity;在pre-验证)
04-05 18:00:11.286:WARN / dalvikvm(1095):无法解析LCOM /例子/安卓/ APP / FragmentLayoutSupport的父类; (49)
04-05 18:00:11.286:WARN / dalvikvm(1095):类的链接LCOM /例子/安卓/ APP / FragmentLayoutSupport;失败
04-05 18:00:11.286:ERROR / dalvikvm(1095):找不到类的com.example.android.app.FragmentLayoutSupport,从法com.example.android.app.test.FrameLayoutTest引用< INIT&GT。 ;
04-05 18:00:11.286:WARN / dalvikvm(1095):VFY:无法解析常量级131(LCOM /例子/安卓/ APP / FragmentLayoutSupport;)在LCOM /例子/安卓/应用/测试/ FrameLayoutTest;
 

下面是code我构建了展示的问题。测试用例只是试图测试来实例化类:

  FrameLayoutTest.java
公共类FrameLayoutTest扩展
            ActivityInstrumentationTestCase2< FragmentLayoutSupport> {
        公共FrameLayoutTest(){
            超(FragmentLayoutSupport.class);
        }

    公共无效testActivityTestCaseSetUpProperly(){
        assertNotNull(活动应成功发射,getActivity());
    }
}
 

是我所创建的两个类如下所示,fragment_layout是空的LinearLayout:

  FrameLayout.java
公共类FragmentLayout延伸活动{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.fragment_layout);
    }
}
 

  FragmentLayoutSupport.java
公共类FragmentLayoutSupport扩展FragmentActivity {
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.fragment_layout);
    }
}
 

解决方案

我花了这半个晚上,终于找到了解决办法。关键的行是:

  04-05 18:00:11.276,(LCOM /例子/安卓/ APP / FragmentLayoutSupport;曾用不同的Landroid /支持/ V4 / APP / FragmentActivity; $ P $中对验证)。
 

问题是,Android的支持,v4.jar您在您的测试项目中使用是从一个应用程序中的项目不同。从测试项目中删除所有引用到Android支持-v4.jar的。然后转到您的应用程序项目属性 - > Java构建Path->订单和出口,并检查Android的支持,v4.jar将它导出。现在,这两个项目都将使用相同​​的库,并达尔维克不会抱怨。

I have a problem executing android unit tests against android applications that utilize the recently released Fragment support API. When the test is run against a FragmentActivity the following error shows up in the log and the class fails to load. When run against an identical class, but one derived from Activity the test works fine. Both classes work correctly as apps! Meaning that when simply invoked they both display their layout and function correctly. The support jar is part of the build path and included in the project.

The problem I have is that the only way to utilize fragments (and support pre3.0 android) is to utilize FragmentActivity, but if that excludes automated testing then what good is this library.

4-05 18:00:11.276: WARN/dalvikvm(1095): Class resolved by unexpected DEX: Lcom/example/android/app/FragmentLayoutSupport;(0x406351a0):0x12e5c8 ref [Landroid/support/v4/app/FragmentActivity;] Landroid/support/v4/app/FragmentActivity;(0x406351a0):0x12e440
04-05 18:00:11.276: WARN/dalvikvm(1095): (Lcom/example/android/app/FragmentLayoutSupport; had used a different Landroid/support/v4/app/FragmentActivity; during pre-verification)
04-05 18:00:11.286: WARN/dalvikvm(1095): Unable to resolve superclass of Lcom/example/android/app/FragmentLayoutSupport; (49)
04-05 18:00:11.286: WARN/dalvikvm(1095): Link of class 'Lcom/example/android/app/FragmentLayoutSupport;' failed
04-05 18:00:11.286: ERROR/dalvikvm(1095): Could not find class 'com.example.android.app.FragmentLayoutSupport', referenced from method com.example.android.app.test.FrameLayoutTest.<init>
04-05 18:00:11.286: WARN/dalvikvm(1095): VFY: unable to resolve const-class 131 (Lcom/example/android/app/FragmentLayoutSupport;) in Lcom/example/android/app/test/FrameLayoutTest;

Here is the code I constructed to demonstrate the issue. The test case simply tries to instantiate the class under test:

FrameLayoutTest.java    
public class FrameLayoutTest extends
            ActivityInstrumentationTestCase2<FragmentLayoutSupport> {
        public FrameLayoutTest() {
            super(FragmentLayoutSupport.class);
        }

    public void testActivityTestCaseSetUpProperly() {
        assertNotNull("activity should be launched successfully", getActivity());
    }
}

The two classes I have created are as follows and fragment_layout is an empty LinearLayout:

FrameLayout.java
public class FragmentLayout extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_layout);
    }
}

And

FragmentLayoutSupport.java
public class FragmentLayoutSupport extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_layout);
    }
}

解决方案

I spent half the night on this, and finally found a solution. The key line is:

04-05 18:00:11.276, (Lcom/example/android/app/FragmentLayoutSupport; had used a different Landroid/support/v4/app/FragmentActivity; during pre-verification). 

The problem is that the android-support-v4.jar which you are using in your test project is different from that one in your application project. Remove all of the references to android-support-v4.jar from your test project. Then go to your application project Properties->Java Build Path->Order and Export and check android-support-v4.jar to export it. Now both projects will be using the same library, and dalvik won't complain.

这篇关于FragmentActivity不能通过ActivityInstrumentationTestCase2测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆