2016-09-27 82 views
1

我遇到了一個奇怪的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

是否有人看到了這只是想知道。它看起來與其他問題類似,但我找到的答案沒有幫助。

+0

使用Activity而不是AppCompatActivity –

+0

@MarkShen我試圖使用DrawerLayout,它只在support-v4中。 –

+0

@MarkShen我將所有片段更改爲不支持片段,並將我的活動更改爲不支持活動。我現在得到一個IllegalStateException:遞歸進入executePendingTransactions。 :/ –

回答

1

從您的配置標籤中刪除清單條目,robolectric會發現它自己。或者在必要時使用位於build目錄中的清單文件。這是一個常見的錯誤,然後robolectric找不到一些東西。

+0

這沒有做任何事情。我將所有片段更改爲非支持片段,並將我的活動更改爲不支持活動。我現在得到一個IllegalStateException:遞歸進入executePendingTransactions。 –

+0

支持庫不僅存在兼容性,還存在於SDK中不可用的新API –

+0

對於遞歸調用問題,我們需要查看代碼。當您添加和替換片段邏輯時,這種錯誤很常見。 – nenick