【问题标题】:Running Android Espresso Test errors with You need to use a Theme.AppCompat theme (or descendant) with this activity运行 Android Espresso 测试错误您需要在此活动中使用 Theme.AppCompat 主题(或后代)
【发布时间】:2016-03-18 14:21:03
【问题描述】:

您好,我正在尝试运行 Espresso 测试,但它失败了,我确定问题是什么或我做错了什么。

当我运行测试时,我会在控制台中看到以下内容

Running tests
Test running started
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.Login.LoginActivity.onCreate(LoginActivity.java:54)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
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:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

它说它失败了,因为我需要在这个活动中使用 Theme.AppCompat 主题(或后代),并显示在堆栈跟踪第 54 行,即 setContentView(R.layout.activity_login);

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

我已经在应用程序中设置了主题,甚至通过在活动上设置主题来确保

   <application android:theme="@style/AppTheme">

  <activity
            android:name=".Login.LoginActivity"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="adjustPan"></activity>

我的主题父是“Theme.AppCompat”

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

所以我有点困惑,为什么当我使用从 Theme.AppCompat 下降的主题时它会失败

这也是我从 AppCompactActivity 扩展的活动

public class LoginActivity extends AppCompatActivity 

我在 Gradle for androidTest 中的依赖项是

androidTestCompile  'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile  'com.android.support.test:runner:0.4.1'
androidTestCompile  'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile 'com.android.support:design:23.1.1'
androidTestCompile 'junit:junit:4.12'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'

这是我要运行的测试

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginNavigationForgotPasswordTest {

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);

    @Test
    public void testClick() {

        // Find Button and Click on it
        onView(withId(R.id.login_forgot_password_button)).perform(click());

        // Find TextView and verify the correct text that is displayed
        onView(withId(R.id.forgot_top_title)).check(matches(withText("Forgot Password")));
    }

}

也许我做错了什么直截了当。

任何帮助将不胜感激。

顺便说一句,应用程序在运行时运行良好

【问题讨论】:

    标签: android testing android-espresso


    【解决方案1】:

    不确定是什么修复了它,但我做了一些更改并设法获得了一个有效的设置。

    1. 更改了“AppTheme”的名称。我认为当 Gradle 构建时,它会与其他在其 styles.xml 中具有相同名称的库混淆,所以我将它重命名为更独特的东西。例如

    之前:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    

    之后:

    <style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    
    1. 我还将 espresso 更新为较新的版本,并确保测试 android 应用程序注释与应用程序注释相同。我还排除了一些模块,如下所述Why would adding espresso-contrib cause an InflateException?

    之前:

    androidTestCompile  'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile  'com.android.support.test:runner:0.4.1'
    androidTestCompile  'com.android.support.test:rules:0.4.1'
    androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
    androidTestCompile 'com.android.support:design:23.1.1'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
    androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
    androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
    androidTestCompile 'org.mockito:mockito-core:1.10.19'
    

    之后:

    compile 'com.android.support:support-annotations:23.2.1'
    androidTestCompile 'com.android.support:support-annotations:23.2.1'
    
        androidTestCompile  'com.android.support.test.espresso:espresso-core:2.2.2'
        androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2'){
            exclude group: 'com.android.support', module: 'appcompat'
            exclude group: 'com.android.support', module: 'support-v4'
            exclude group: 'com.android.support', module: 'design'
    
            exclude group: 'com.github.afollestad.material-dialogs', module: 'core'
            exclude group: 'com.github.afollestad.material-dialogs', module: 'commons'
            exclude group: 'com.github.traex.rippleeffect', module:'library'
            exclude group: 'com.balysv', module:'material-ripple'
    
            exclude module: 'recyclerview-v7'
        }
        androidTestCompile ('com.android.support.test:runner:0.5'){
        }
        androidTestCompile ('com.android.support.test:rules:0.4.1'){
        }
    
        androidTestCompile ('junit:junit:4.12')
    
        androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
        androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
        androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
    
        androidTestCompile 'org.mockito:mockito-core:1.10.19'
    

    我还添加了以下分辨率策略

    configurations.all{
        resolutionStrategy {
            force 'org.hamcrest:hamcrest-core:1.1', 'org.hamcrest:hamcrest-core:1.3'
        }
    
    }
    

    事后看来,我也可以通过这种方式添加支持注释

    configurations.all{
        resolutionStrategy {
            force 'com.android.support:support-annotations:23.2.1','org.hamcrest:hamcrest-core:1.1', 'org.hamcrest:hamcrest-core:1.3'
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,但对我来说,解决方案只是在 AndroidTest 的清单中放置一个基于 AppCompat 的主题,就像我为 Android 应用程序清单设置的一样。对于我的项目,应用程序清单不用于 Espresso 测试,而是使用 AndroidTest 清单。

      【讨论】:

        【解决方案3】:

        如果您不想弄乱您的主题设置或清单,那么有一个更简单的解决方案。

        在运行时设置主题,如下所示:

        @Before
          fun setUp() {
            context = InstrumentationRegistry.getInstrumentation().targetContext
            context.setTheme(R.style.Theme_AppCompat_Light)
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-03-15
          • 2018-11-27
          • 2015-11-27
          • 2016-07-04
          • 2016-05-16
          相关资源
          最近更新 更多