Robolectric IllegalState异常

时间:2016-09-27 05:11:22

标签: java android android-gradle robolectric

我遇到了一个奇怪的IllegalStateException,我认为这是Robolectric的一个问题。这是堆栈跟踪:

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:340)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:309)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:273)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.sr.dd.dd_app.screens.main.MainActivity.onCreate(MainActivity.java:87)

MainActivity.java第87行

setContentView(R.layout.activity_main);

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The main content view --> <FrameLayout android:id="@+id/main_content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- The navigation drawer --> <ListView android:id="@+id/main_left_drawer" android:layout_width="280dp" android:layout_height="match_parent" android:layout_gravity="left|start" android:background="#FFFFFF" /> </android.support.v4.widget.DrawerLayout>

实例化Activity的单元测试:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, shadows = {ShadowGoogleApiAvailability.class}, manifest="app/src/main/AndroidManifest.xml")
public class ScreenUnitTest {
    ... //Some unimportant stuff
    @Before
    public void setUp() {
        ... //More unimportant stuff
        mainActivity = Robolectric.setupActivity(MainActivity.class);
    }
}

的build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'org.mockito:mockito-core:1.+'
    androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
    androidTestCompile 'com.android.support:support-annotations:23.3.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.+'
    testCompile 'org.robolectric:robolectric:3.1'
    testCompile "org.robolectric:shadows-support-v4:3.1"
    testCompile "org.robolectric:shadows-play-services:3.1"
    testCompile 'org.robolectric:shadows-maps:3.1'
    testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
    testCompile 'com.android.support:design:23.3.0'

    compile 'com.android.support:design:23.3.0'
    compile 'com.android.support:support-annotations:23.3.0'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:support-v4:23.3.0'
    compile 'com.google.android.gms:play-services:8.1.0'
    compile 'com.android.support:design:23.0.+'
    compile 'com.android.support:cardview-v7:23.0.+'
    compile 'com.android.support:recyclerview-v7:23.0.+'
    compile 'joda-time:joda-time:2.9.4'
}

使用Sdk 23版进行编译

只是想知道是否有其他人见过这个。它看起来与其他问题类似,但我发现的答案没有帮助。

1 个答案:

答案 0 :(得分:1)

从配置标签中删除清单条目,robolectric会自行查找。或者在必要时使用构建目录中的清单文件。这是一个常见的错误,然后robolectric找不到一些东西。