簡體   English   中英

當我在Roboelectric測試中檢查運行服務時,Nullpointerexception

[英]Nullpointerexception when I check for running service in Roboelectric test

當我對onCreate()方法檢查某個特定服務是否正在運行的活動運行測試時,我得到一個NPE。 在測試的設置方法中,我調用:

ActivityController<MyActivity> ac = Robolectric.buildActivity(MyActivity.class).create();
    mActivity = ac.get();

在活動的onCreate()方法中,我調用isServiceRunning()

ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    if(manager != null) {
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (service.service != null && "MyService".equals(service.service.getClassName())) {
                return true;
            }
        }
    }
    return false;

執行此操作時,NPE失敗:

for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))

manager不是null,但NPE在android庫中的ActivityManager.getRunningServices(int)中拋出。

如果我需要這種方法通過,我該怎么辦? 我正在使用Roboelectric 2.2-20130612.001729-6-jar-with-dependencies.jar但我現在看到問題,即使是最新版本。

我使用Android SDK版本19,但我現在切換到18,我仍然看到問題。

有任何想法嗎?

堆棧跟蹤:

java.lang.NullPointerException  
at android.app.ActivityManager.getRunningServices(ActivityManager.java:1106)
at com.example.MyActivity.isServiceRunning(MyActivity.java:151)
at com.example.MyActivity.onCreate(MyActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5133)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:116)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:257)
at org.robolectric.util.ActivityController.create(ActivityController.java:111)
at org.robolectric.util.ActivityController.create(ActivityController.java:123)
at com.example.test.MyActivityTest.setUp(MyActivityTest.java:44)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:233)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:174)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
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)

您需要更改if語句以使其更具包容性。 從android文檔上的功能:
注意:此方法僅用於調試或實現服務管理類型用戶界面。

所以我建議你非常小心你的空檢查(注意復數)。 下面的順序很重要,或者您可能會在if語句的條件中收到空指針。

  1. 確保經理不是空的然后
  2. 確保List <>不為null然后
  3. 確保不是空的。
    (null和empty之間的巨大差異,例如:我們不能在null List上運行.isEmpty()函數)。

if(manager != null && manager.getRunningServices(MAX) != null && !manager.getRunningServices(MAX).isEmpty() != null)

旁注:更容易閱讀, 第1號檢查之后,您將服務列表保存到變量,並在該變量上只有第二個 if語句, 然后對第二個if語句中的for each語句執行操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM