我遇到了一個奇怪的IllegalStateException,我認爲這是Robolectric的問題。這是堆棧跟蹤: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>
單元測試,其中活性被實例化:
@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
是否有人看到了這只是想知道。它看起來與其他問題類似,但我找到的答案沒有幫助。
使用Activity而不是AppCompatActivity –
@MarkShen我試圖使用DrawerLayout,它只在support-v4中。 –
@MarkShen我將所有片段更改爲不支持片段,並將我的活動更改爲不支持活動。我現在得到一個IllegalStateException:遞歸進入executePendingTransactions。 :/ –