2017-06-20 88 views
4

我使用Robolectric 3.3com.android.support:appcompat-v7:23.4.0
测试AppCompatActivity后我得到:Robolectric 3.3 Theme.AppCompat

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 

我创建了下面的测试运行:

public class ActivityTestRunner extends RobolectricTestRunner { 

    public ActivityTestRunner(Class<?> testClass) throws InitializationError { 
     super(testClass); 
    } 

    @Override 
    protected AndroidManifest getAppManifest(Config config) 
    { 
     return new AppManifest(Fs.fileFromPath(config.manifest()), 
       Fs.fileFromPath(config.resourceDir()), 
       Fs.fileFromPath(config.assetDir())); 
    } 

    private class AppManifest extends AndroidManifest 
    { 
     private static final String THEME = "@style/Theme.AppCompat"; 


     public AppManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory) { 
      super(androidManifestFile, resDirectory, assetsDirectory); 
     } 

     public AppManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory, String overridePackageName) { 
      super(androidManifestFile, resDirectory, assetsDirectory, overridePackageName); 
     } 

     @Override 
     public String getThemeRef() { 
      return THEME; 
     } 

     @Override 
     public String getThemeRef(String activityClassName) { 
      return THEME; 
     } 
    } 
} 

然后我m得到:

java.lang.NullPointerException 
    at org.robolectric.res.ThemeStyleSet$OverlayedStyle.equals(ThemeStyleSet.java:67) 
    at org.robolectric.res.ThemeStyleSet.apply(ThemeStyleSet.java:29) 
    at org.robolectric.shadows.ShadowAssetManager.applyThemeStyle(ShadowAssetManager.java:595) 
    at org.robolectric.shadows.ShadowAssetManager.applyThemeStyle(ShadowAssetManager.java:588) 
    at android.content.res.AssetManager.applyThemeStyle(AssetManager.java) 
    at android.content.res.Resources$Theme.applyStyle(Resources.java:1104) 
    at android.view.ContextThemeWrapper.onApplyThemeResource(ContextThemeWrapper.java:132) 
    at android.app.Activity.onApplyThemeResource(Activity.java:3307) 
    at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:144) 
    at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:89) 
    at android.support.v7.app.AppCompatActivity.setTheme(AppCompatActivity.java:90) 
    at org.robolectric.shadows.ShadowActivity.setThemeFromManifest(ShadowActivity.java:89) 
    at org.robolectric.shadows.CoreShadowsAdapter$1.setThemeFromManifest(CoreShadowsAdapter.java:30) 
    at org.robolectric.android.controller.ActivityController.attach(ActivityController.java:84) 
    at org.robolectric.android.controller.ActivityController.of(ActivityController.java:34) 
    at org.robolectric.Robolectric.buildActivity(Robolectric.java:93) 
    at org.robolectric.Robolectric.buildActivity(Robolectric.java:89) 
    at org.robolectric.Robolectric.setupActivity(Robolectric.java:97) 
    at com.lacoon.components.activities.MainActivityTest.setUp(MainActivityTest.java:35) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 
    at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:209) 
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109) 
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:63) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

风格:

<style name="AppTheme" parent="Theme.AppCompat"> 
     <!-- Customize your theme here. --> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
     <item name="android:textColor">@color/white</item> 
     <item name="colorPrimary">@color/main_background</item> 
     <item name="colorPrimaryDark">@color/main_background</item> 
     <item name="android:textViewStyle">@style/RobotoLightTextViewStyle</item>" 
     <item name="colorAccent">@color/check_point_pink</item> 
     <item name="colorControlNormal">#9a9a9a</item> 
     <item name="colorControlActivated">@color/check_point_pink</item> 
     <!--<item name="editTextStyle">@style/welcome_editbox</item>--> 
     <!--<item name="alertDialogStyle">@style/dialog_theme</item>--> 
    </style> 

的onCreate:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main_activity); 
     getMainScreenFragment(); 
     mToolbar = (Toolbar) findViewById(toolbar); 
     setUpToolBar(); 
    } 

如何使它工作?

+0

添加它并没有帮助我,但你可以尝试https://github.com/robolectric/robolectric/issues/2949 –

回答

1

的问题是,我没加@Config(constants = BuildConfig.class)
必须按tutorial

-2

您遇到此问题的原因是因为您尝试应用对话框主题的活动正在扩展需要应用AppCompat主题的ActionBarActivity。

更新:扩展AppCompatActivity也有这个问题

在这种情况下,从ActionBarActivity改变Java继承到活动并退出对话框主题清单,因为它是一个非Theme.AppCompat值

一般规则是,如果您希望代码支持较旧版本的Android,则应具有AppCompat主题,并且Java代码应扩展AppCompatActivity。如果您的*活动不需要这种支持,例如您只关心Android的最新版本和功能,则可以将任何主题应用于该主题,但Java代码必须扩展普通的旧Activity。

注意:从AppCompatActivity(或子类,ActionBarActivity)更改为Activity时,还必须将“支持”的各种呼叫更改为没有“支持”的相应呼叫。因此,而不是getSupportFragmentManager,请调用getFragmentManager。

https://stackoverflow.com/a/21815015/8148100

+0

请再读一遍这个问题。 – NickF

+0

@NickF你可以发布该活动和style.xml的代码? –

+0

查看问题主体 – NickF

1

只是删除您定制的运行并解决问题Robolectric测试路径。检查如何去做here

+0

中的更新我已经通过了该部分。 – NickF