因爲Robolectric是在項目開發過程當中引入的,因此要跑起來時就遇到了各類問題,下面整理一下運行過程當中我的遇到的問題和解決方法。 java
1、 android
現象: app
java.lang.IllegalStateException: there must have been some overlap for resourceIdToResName! expected 4897 but got 4896 at org.robolectric.res.MergedResourceIndex.merge(MergedResourceIndex.java:25) at org.robolectric.res.MergedResourceIndex.<init>(MergedResourceIndex.java:17) at org.robolectric.res.RoutingResourceLoader.<init>(RoutingResourceLoader.java:22) at org.robolectric.RobolectricTestRunner.createAppResourceLoader(RobolectricTestRunner.java:601) at org.robolectric.RobolectricTestRunner.getAppResourceLoader(RobolectricTestRunner.java:585) at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:89) at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:404) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:220) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
解決方法: eclipse
這是是因爲在部分的layout xml中,一些「android:id="@android :id/**"」致使的,具體緣由和具體哪些xml致使,我不清楚。因爲項目中多個xml使用了「@android:id/**」,爲了程序的穩定和不遺漏任何地方,我把xml中全部沒必要要的「@android :id/**」改成「@+id/**」或者"@id/**"(注意:TabHost對象及其相關對象必選使用「@android :id/tabs」),同時把java文件對應的讀取方法改掉。 oop
2、Looper.getMainLooper() ui
現象: lua
Caused by: java.lang.NullPointerException at org.robolectric.shadows.ShadowLooper.getMainLooper(ShadowLooper.java:59) at android.os.Looper.getMainLooper(Looper.java) at com.example.app.MyApplication.<clinit>(MyApplication.java:277) ... 25 more
解決方法:(去掉Looper.getMainLooper()) spa
//private static Handler handler = new Handler(Looper.getMainLooper()) { //for Robolectric Unit Test private static Handler handler = new Handler() {
3、 .net
現象: code
java.lang.NullPointerException at android.app.Activity.onCreate(Activity.java:874) at com.example.activity.SplashScreenActivity.onCreate(SplashScreenActivity.java:39) at com.example.activity.SplashScreenActivityTest.setUp(SplashScreenActivityTest.java:29) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
private SplashScreenActivity activity; @Before public void setUp() { // activity = new SplashScreenActivity(); // Intent newIntent = new Intent(); // activity.setIntent(newIntent); // activity.onCreate(null); activity = Robolectric.buildActivity(SplashScreenActivity.class) .create().get(); }