空指针异常出发IntentService指针、异常、IntentService

2023-09-05 02:01:30 作者:邮个拥抱

我已经得到了我想要启动IntentService。当我这样做,它吐出了这一点:

I've got an IntentService that I'm trying to start. When I do, it spits out this:

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

我用Google搜索这个看着像许多同类的StackOverflow问题,我能找到。不过,也有几个细微的差别,我不能环绕我的头。首先,没有任何我的课在异常引用的。其次,类似的问题已得到修复,通过改变环境或双重检查,以确保它不为空。

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.

我有code,以检查是不是这样的:

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)
        }
    });

}

我IntentService是谷歌文件为蓝本。一个简单的构造与onHandleIntent方法。

My IntentService is modelled after the google docs. Simply a constructor with an onHandleIntent method.

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...
}

所以我的问题是这样的:这哪里是例外,来自哪里,是有什么我可以做不同的,以避免它我的谷歌福没有让我失望,在过去,所以希望这?是不是那些非常明显的答案之一。此外,如果有事情可以做得更好,或只是普通的丑陋的,建设性的批评总是AP preciated。

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.

我把全部的例外,NewsActivity和NewsService对引擎收录的情况下,我离开的东西了。 http://pastebin.com/mR9Sykrq

I put the full exception, NewsActivity, and NewsService on pastebin in case I left something out. http://pastebin.com/mR9Sykrq

推荐答案

如果你要重写的onCreate() IntentService ,然后确保你叫 super.onCreate()在里面。这似乎很可能是你的问题。

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.