大佬教程收集整理的这篇文章主要介绍了android – Service.startForeground()在使用ServiceTestCase运行时抛出NullPointerException,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的问题是除了修改Android源代码并再次编译之外,有没有人知道这个问题的解决方法?
这是我的代码,为了完成:
/** Shows notification of started service */ private void doStartForeground() { // Prepare notification final NotificationHelper nh = doNotification("service started"); // Start foreground startForeground(nh.getNotificationId(),nh.getNotification()); }
和JUnit测试方法:
public void test1() throws InterruptedException { assertTrue(context != @R_673_6633@ final Intent service = new Intent(); service.setComponent(new ComponentName(Coreservice.PACKAGE_NAME,Coreservice.serviCE_FULL_Name)); IBinder binder = bindservice(servicE); assertTrue(binder != @R_673_6633@ }
堆栈跟踪:
java.lang.NullPointerException at 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;
// Start the service as foreground (Android should not kill this // servicE) if (!isJUnit) { doStartForeground(); }
然后我从JUnit的setUp()设置这个标志:
@Override protected void setUp() throws Exception { super.setUp(); // More setup MyserviceClass.isJUnit = true; }
不是最优雅的解决方案,但让我摆脱封锁.
以上是大佬教程为你收集整理的android – Service.startForeground()在使用ServiceTestCase运行时抛出NullPointerException全部内容,希望文章能够帮你解决android – Service.startForeground()在使用ServiceTestCase运行时抛出NullPointerException所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。