ShihooWang / DaemonLibrary

Android后台保活,这里有你需要的所有姿势。2019,最新版本。
393 stars 82 forks source link

工作进程改到主进程后我的项目报错了,运行了下demo是没问题的 #15

Open yk007 opened 5 years ago

yk007 commented 5 years ago

E/ActivityThread: Service hxht.megaeyes.service.MainWorkService has leaked ServiceConnection com.shihoo.daemon.work.AbsWorkService$1@a4437ae that was originally bound here android.app.ServiceConnectionLeaked: Service hxht.megaeyes.service.MainWorkService has leaked ServiceConnection com.shihoo.daemon.work.AbsWorkService$1@a4437ae that was originally bound here at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:1804) at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1695) at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1892) at android.app.ContextImpl.bindService(ContextImpl.java:1845) at android.content.ContextWrapper.bindService(ContextWrapper.java:698) at com.shihoo.daemon.DaemonEnv.startServiceMayBind(DaemonEnv.java:41) at com.shihoo.daemon.work.AbsWorkService.onStart(AbsWorkService.java:76) at com.shihoo.daemon.work.AbsWorkService.onStartCommand(AbsWorkService.java:60) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4271) at android.app.ActivityThread.access$2700(ActivityThread.java:273) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2056) at android.os.Handler.dispatchMessage(Handler.java:112) at android.os.Looper.loop(Looper.java:216) at android.app.ActivityThread.main(ActivityThread.java:7593) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

Caused by: java.lang.NullPointerException: Attempt to read from field 'android.os.MessageQueue android.os.Looper.mQueue' on a null object reference at android.os.Handler.(Handler.java:244) at android.os.Handler.(Handler.java:147) at android.location.LocationManager$GnssStatusListenerTransport$GnssHandler.(LocationManager.java:1773) at android.location.LocationManager$GnssStatusListenerTransport.(LocationManager.java:1835) at android.location.LocationManager$GnssStatusListenerTransport.(LocationManager.java:1830) at android.location.LocationManager.addGpsStatusListener(LocationManager.java:2031) at hxht.megaeyes.utils.LocationUtils.getLocation(LocationUtils.java:92) at hxht.megaeyes.utils.LocationUtils.(LocationUtils.java:35) at hxht.megaeyes.utils.LocationUtils.getInstance(LocationUtils.java:43) at hxht.megaeyes.service.MainWorkService.sendLocation(MainWorkService.java:162) at hxht.megaeyes.service.MainWorkService.access$100(MainWorkService.java:41) at hxht.megaeyes.service.MainWorkService$1.startTimerTask(MainWorkService.java:155) at hxht.megaeyes.service.MainWorkService$1.accept(MainWorkService.java:128) at hxht.megaeyes.service.MainWorkService$1.accept(MainWorkService.java:122) at io.reactivex.internal.observers.LambdaObserver.onNext(LambdaObserver.java:63)

yk007 commented 5 years ago

92行写的是 //监听gps状态 locationManager.addGpsStatusListener(listener);没想明白为什么会空指针

ShihooWang commented 5 years ago

这个要看你的MainWorkService:162这一行是咋写的 at hxht.megaeyes.service.MainWorkService.sendLocation(MainWorkService.java:162)

yk007 commented 5 years ago

162: sendLocation();//发送实时定位数据 private void sendLocation(){ LocationUtils locationUtils = LocationUtils.getInstance(getApplicationContext()); Location location=locationUtils.showLocation(); if(location!=null){ ServiceRequest request = new ServiceRequest(MainWorkService.this, null, new IResponseCallBack() {

            @Override
            public void executeResponsedAction(Void dataObject, Boolean isSuccess, String responseMessage) {
                if(isSuccess){
                    LogToFileUtils.write("定位数据上报成功");
                }else{
                    //弹出相应对话框和对应处理
                    LogToFileUtils.write(responseMessage);
                }
            }

            @Override
            public void executeResponsedActionForLogin(String dataObject, Boolean isSuccess, String responseMessage) {

            }

            @Override
            public void onFailure(HttpException e, Response<String> response) {
                LogToFileUtils.write("onFailure:"+e.getMessage());
            }
        });

        String idCard= (String) SPUtils.get(Constants.USERDATA,this,Constants.IDCARD,"");
        if(UrlConstant.isDebug){
            idCard=UrlConstant.ID_CARD;
        }
        String url = URLManager.createGpsUpUrl(UrlConstant.LOCATION_IP,UrlConstant.LOCATION_PORT,idCard,location.getLatitude()+"",location.getLongitude()+"");
        LogToFileUtils.write("url="+url);
        try {
            request.performanceURLQueryNoneData(url);

        } catch (Exception ex) {
            LogToFileUtils.write( ex.getMessage());
        }

    }
}

public static LocationUtils getInstance(Context context) { if (uniqueInstance == null) { synchronized (LocationUtils.class) { if (uniqueInstance == null) { uniqueInstance = new LocationUtils( context ); } } } return uniqueInstance; }

请指教,谢谢

ShihooWang commented 5 years ago

静态、单例在不同进程中会失效,这可能会导致空指针。请再确认下MainWorkService的进程和LocationUtils 初始化时的进程,是不是同一个进程。

yk007 commented 5 years ago

是在主进程的, Caused by: java.lang.NullPointerException: Attempt to read from field 'android.os.MessageQueue android.os.Looper.mQueue' on a null object reference主要是这个错误第一次遇到