我的问题是除了修改AndroID源代码并再次编译之外,有没有人知道这个问题的解决方法?
这是我的代码,为了完成:
提升NPE的My Service子类方法:
/** Shows notification of started service */private voID doStartForeground() { // Prepare notification final NotificationHelper nh = doNotification("Service started"); // Start foreground startForeground(nh.getNotificationID(),nh.getNotification());}
这是从onCreate()方法覆盖调用的.
和JUnit测试方法:
public voID test1() throws InterruptedException { assertTrue(context != null); final Intent service = new Intent(); service.setComponent(new Componentname(CoreService.PACKAGE_name,CoreService.SERVICE_FulL_name)); IBinder binder = bindService(service); assertTrue(binder != null);}
堆栈跟踪:
java.lang.NullPointerExceptionat androID.app.Service.startForeground(Service.java:631)at com.blablabla.androID.core.CoreService.doStartForeground(CoreService.java:82)at com.blablabla.androID.core.CoreService.onCreate(CoreService.java:149)at androID.test.ServiceTestCase.bindService(ServiceTestCase.java:234)at com.blablabla.androID.core.test.MainTest.test1(MainTest.java:37)at java.lang.reflect.Method.invokeNative(Native Method)at androID.test.AndroIDTestRunner.runTest(AndroIDTestRunner.java:169)at androID.test.AndroIDTestRunner.runTest(AndroIDTestRunner.java:154)at androID.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)at androID.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
服务启动正常,并且在不通过AndroID的JUnit运行时工作正常.
解决方法 我终于在我的服务中添加了一个新的静态字段:/** JUNIT - this is for testing purposes only */public static boolean isJUnit = false;
并在onCreate()方法中检查它:
// Start the service as foreground (AndroID should not kill this// service)if (!isJUnit) { doStartForeground();}
然后我从JUnit的setUp()设置这个标志:
@OverrIDeprotected voID setUp() throws Exception { super.setUp(); // More setup MyServiceClass.isJUnit = true;}
不是最优雅的解决方案,但让我摆脱封锁.
总结以上是内存溢出为你收集整理的android – Service.startForeground()在使用ServiceTestCase运行时抛出NullPointerException全部内容,希望文章能够帮你解决android – Service.startForeground()在使用ServiceTestCase运行时抛出NullPointerException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)