以明确的意图NullPointerException启动Android IntentService [英] Starting Android IntentService with explicit intent NullPointerException
问题描述
我正在尝试从主活动中的clickhandler启动 IntentService
。我现在正在学习 Intents
,但还没有完全掌握它。我不确定我应该如何在这里实例化我的意图并将其传递给 startService
。我不知道为什么我必须这样做, Intent
将不会在我的服务中使用,我知道。我不知道如何调试这个问题。
I'm trying to start an IntentService
from a clickhandler in my main activity. I'm studying Intents
right now but don't quite get it yet. I'm not sure how I'm supposed to instantiate my intent here and pass it to startService
. I don't know why I have to do such a thing, the Intent
will not be used within my service, that I know of. I don't know how to debug this problem.
点击回调:
this.commute = new Commute();
locationService = new LocationService();
locationService.setCommute(commute);
// com.orm.SugarApp@8b2e18f is this.getApplicationContext() ...
Context context = this.getBaseContext();
System.out.println("!!!!!!!!!!!!!!!!!!!!!");
System.out.println(context);
Intent intent = new Intent(context, LocationService.class);
System.out.println(locationService);
System.out.println(intent);
locationService.startService(intent); // <---- OFFENDING LINE
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@");
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
commute.start();
回溯前的logcat,没有任何对象 null
...:
logcat before traceback, none of the objects are null
...:
01-27 09:53:44.465 2718-2718/org.skyl.commutetracker I/System.out﹕ !!!!!!!!!!!!!!!!!!!!!
01-27 09:53:44.466 2718-2718/org.skyl.commutetracker I/System.out﹕ android.app.ContextImpl@8b2e18f
01-27 09:53:44.466 2718-2718/org.skyl.commutetracker I/System.out﹕ org.skyl.commutetracker.services.LocationService@387dcc1c
01-27 09:53:44.466 2718-2718/org.skyl.commutetracker I/System.out﹕ Intent { cmp=org.skyl.commutetracker/.services.LocationService }
01-27 09:53:44.466 2718-2718/org.skyl.commutetracker D/AndroidRuntime﹕ Shutting down VM
01-27 09:53:44.481 2718-2718/org.skyl.commutetracker E/AndroidRuntime﹕ FATAL EXCEPTION: main
这会导致以下回溯:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference
at android.content.ContextWrapper.startService(ContextWrapper.java:515)
at org.skyl.commutetracker.MainActivity.startCommute(MainActivity.java:86)
at org.skyl.commutetracker.MainActivity.toggleClick(MainActivity.java:67)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
推荐答案
您不希望在第二行实例化LocationService。
You would not want to instantiate LocationService on the second line.
您可能希望使用以下某项之一:
You would want to use one of the following:
context.bindService(intent, serviceConnection, boolean);
或
context.startService(intent);
对于您的IntentService,您可能希望使用startService。绑定服务与意向服务不同。
For your IntentService, you would want to use startService. Bound services are different than intent services.
此外,如果您想将通勤传递到意向服务,您可能希望将其作为额外服务传递给意图。
Also, if you want to pass the commute into the intent service, you would want to pass it into the intent as an extra.
intent.putExtra(String key, Parcelable item)
putExtra被重载,因此只要Commute对象实现了一个必需的接口,它就可以正常工作。有关Intent对象的更多信息,请访问 http://developer.android.com /reference/android/content/Intent.html
The putExtra is overloaded, so as long as the Commute object implements one of the required interfaces, it should work fine. More information about the Intent object can be found at http://developer.android.com/reference/android/content/Intent.html
您可以在此处找到有关服务的更多信息 http://developer.android.com/guide/components/services.html
You can find more information about services here http://developer.android.com/guide/components/services.html
这篇关于以明确的意图NullPointerException启动Android IntentService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!