java 启动 IntentService 的空指针异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7165215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Null Pointer exception starting IntentService
提问by Trev
I've got an IntentService that I'm trying to start. When I do, it spits out this:
我有一个我正在尝试启动的 IntentService。当我这样做时,它会吐出这个:
java.lang.RuntimeException: Unable to start service com.pec.testapp.service.NewsService@406bd940 with Intent { cmp=com.pec.testapp/.service.NewsService }: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2173)
... (omitted for brevity)
Caused by: java.lang.NullPointerException
at android.app.IntentService.onStart(IntentService.java:110)
at android.app.IntentService.onStartCommand(IntentService.java:118)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2160)
... 10 more
I have googled this and looked at as many similar StackOverflow questions as I could find. However, there are a couple of subtle differences that I can't wrap my head around. First of all, there aren't any of my classes referenced in the exception. Secondly, similar questions have been fixed by changing the context or double checking to make sure it's not null.
我用谷歌搜索了这个,并尽可能多地查看了类似的 StackOverflow 问题。但是,有一些细微的差异是我无法理解的。首先,异常中没有引用我的任何类。其次,类似的问题已通过更改上下文或双重检查以确保它不为空来解决。
I have code to check that isn't the case:
我有代码来检查情况并非如此:
public Context context;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
context = getApplicationContext();
if(context == null)
Log.d("PECAPP","Context is null");
setContentView(R.layout.news_layout);
...Omit button code...
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view){
Intent i = new Intent(context, NewsService.class); // also tried NewsActivity.this
if( i != null) // I know that this should never happen but I'm at a loss...
startService(i); // I've also tried this with context.startService(i)
}
});
}
My IntentService is modelled after the google docs. Simply a constructor with an onHandleIntent method.
我的 IntentService 是在 google docs 之后建模的。只是一个带有 onHandleIntent 方法的构造函数。
public NewsService() {
super("NewsService");
}
...omit onCreate() and onDestroy() since they haven't been implemented yet...
@Override
protected void onHandleIntent(Intent intent) throws IllegalArgumentException {
Log.d("PECAPP","Got here..."); // I never actually got here...
if(intent == null) Log.d("PECAPP","INTENT IS NULL");
...omit rest of code...
}
So my question is this: Where is this exception coming from and is there something I can do different to avoid it?My google-fu hasn't failed me in the past, so hopefully this isn't one of those painfully obvious answers. Also, if there are things that can be done better or are just plain ugly, constructive criticism is always appreciated.
所以我的问题是:这个异常来自哪里,我可以做些什么来避免它?我的 google-fu 过去并没有让我失望,所以希望这不是那些令人痛苦的明显答案之一。此外,如果有些事情可以做得更好,或者只是很丑陋,建设性的批评总是受到赞赏。
I put the full exception, NewsActivity, and NewsService on pastebin in case I left something out. http://pastebin.com/mR9Sykrq
我把完整的异常、NewsActivity 和 NewsService 放在 pastebin 上,以防我遗漏了一些东西。http://pastebin.com/mR9Sykrq
回答by kabuko
If you're going to override onCreate()
in your IntentService
, then make sure you call super.onCreate()
in it. That seems to quite likely be your problem.
如果您要覆盖onCreate()
您的IntentService
,请确保您调用super.onCreate()
它。这似乎很可能是你的问题。
回答by guyland123
Not sure if it is the same problem, but I was using an intentService also, and I was having trouble using
不确定它是否是同样的问题,但我也使用了一个 intentService,而且我在使用时遇到了问题
context = getApplicationContext();
or context = getBaseContext(); I wasn't overriding onCreate so the previous solution may be the solution for you, I was working inside "onHandleIntent"
或 context = getBaseContext(); 我没有覆盖 onCreate 所以以前的解决方案可能是你的解决方案,我在“onHandleIntent”中工作
I would get an immediate exception with the first, and a exception later when I tried using the 2nd.
我会立即收到第一个异常,稍后尝试使用第二个时会出现异常。
I ended up realizing that the Intentservice is itself a subclass of Context, so I substituted "this" wherever I needed an instance of "Context".
我最终意识到 Intentservice 本身就是 Context 的子类,所以我在需要“Context”实例的地方替换了“this”。
This solved my problem.
这解决了我的问题。