服务onStartCommand抛出NullPointerException [英] Service onStartCommand throwing NullPointerException

查看:90
本文介绍了服务onStartCommand抛出NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行两个单独服务的应用程序.其中一个运行平稳,而另一个在大多数情况下都正常运行,但是在许多更新的设备(Samsung,HTC,LG)上,间歇地在onStartCommand()内的NullPointerException中间歇地抛出一个NullPointerException,而这些设备之间的Android版本不同和4.4.2.我无法在我自己的任何设备上重现该错误,包括遇到此问题的两个确切模型. stacktrace如下,指示问题在第145行:

I have an app that runs two separate services. One of them runs smoothly, and the other works without issue a majority of the time, but is throwing a NullPointerException within onStartCommand() intermittently on a significant number of newer devices (Samsung, HTC, LG) on varying Android versions between 4.1 and 4.4.2. I am unable to reproduce the error on any of my own devices, including two of the exact models which have experienced this issue. The stacktrace is as follows, indicating the problem is on line 145:

java.lang.RuntimeException: Unable to start service com.mycom.myapp.MyLocService@425ab718
with Intent { cmp=com.mycom.myapp/.MyLocService }: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2695)
at android.app.ActivityThread.access$1900(ActivityThread.java:146)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5168)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mycom.myapp.MyLocService.onStartCommand(MyLocService.java:145)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2678)
... 10 more


我正在使用此处提供的答案中链接的一部分代码. com/users/2620320/blackcj> blackcj 在我的应用程序后台运行定位服务;这是MyLocService.java的相关代码段,其中有问题的行标记为:


I am using a portion of the code linked within the answer provided here by blackcj to run a location service in the background of my app; here is the relevant snippet from MyLocService.java, with the offending line marked:

@Override
public void onCreate() {
    super.onCreate();

    mInProgress = false;
    mLocationRequest = LocationRequest.create();

    servicesAvailable = servicesConnected();
    mLocationClient = new LocationClient(this, this, this);
    locIntent = new Intent(MY_LOCATION);
}

public int onStartCommand (Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);      // <----- NPE AT LINE 145

    setUpLocationClientIfNeeded();

    if(!mLocationClient.isConnected() || !mLocationClient.isConnecting()) {
        mLocationClient.connect();
    }

    return START_STICKY;
}

private void setUpLocationClientIfNeeded() {
    if(mLocationClient == null) {
            mLocationClient = new LocationClient(this, this, this);
    }
}


该服务由MyActivity.java中onCreate()中的以下代码启动:


The service is started by the following code within onCreate() in MyActivity.java:

Intent MyLocService = new Intent(this, MyLocService.class);     
startService(MyLocService);


该服务在AndroidManifest.xml的<application>元素中声明,如下所示:


The service is declared within the <application> element in AndroidManifest.xml as follows:

    <service
         android:name=".MyLocService"
         android:singleUser="true"
         android:stopWithTask="false" >
         <intent-filter>
             <action android:name="com.mycom.myapp.MyLocService" />
         </intent-filter>
     </service>


我无法确定导致此异常的原因,因为该异常不会持续发生,并且我无法自己重现.是什么原因导致此间歇性问题?


I have been unable to determine what is causing this exception, as it does not happen consistently and I cannot reproduce it myself. What could be causing this intermittent problem?

推荐答案

看来,返回START_REDELIVER_INTENT而不是START_STICKY已解决了此问题.我没有继续寻找如何检查空意图的方法,而是找到了一种方法,该方法原本不会引起这种情况的.

It appears that returning START_REDELIVER_INTENT instead of START_STICKY has resolved this problem. Instead of continuing to figure out how to check for a null intent, I found a method that supposedly will not cause it in the first place.

随着该方法在我的用户中得到越来越广泛的实现,我将提供我发现的有关该方法的任何进一步结论.

I will update with any further conclusions I discover regarding this method as it becomes more widely implemented by my users.

这篇关于服务onStartCommand抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆