IntentService意图为null并抛出networkonmainthread

时间:2015-10-09 21:13:07

标签: java android android-intent

我将从GCM服务器收到的GcmIntentService数据直接发送到使用intent的IntentService。但是我的意图在IntentService的onHandleIntent方法中为null。我在IntentService中有OnLocationChanged方法,它将那些字符串传递给那个null intent,奇怪的是它们不是null。

@Override
        public void onMessageReceived(String from, Bundle data) {

                if (data != null && !data.isEmpty()) {//this is for the initial updates before any requests by the user
                    if ( data.containsKey("ipaid")){
                        String passenger = data.getString("ipaid");

                        Intent iniupdates = new Intent(MyGcmListenerService.this, InitialLocationUpdater.class);
                        iniupdates.putExtra(InitialLocationUpdater.PARAM_IN_P, passenger);
                        iniupdates.putExtra(InitialLocationUpdater.PARAM_IN_D, tok);
                        iniupdates.putExtra("lat", la);
                        iniupdates.putExtra("lon", lo);
                        startService(iniupdates); 
                    }
}

@Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null && mLastLocation != null) {
            pa = intent.getStringExtra(PARAM_IN_P); <----here pa and dr are null
            dr = intent.getStringExtra(PARAM_IN_D);

            sendLocation(dr, String.valueOf(mLastLocation.getLatitude()), String.valueOf(mLastLocation.getLongitude()), pa); <--this throws npe b/c dr and pa
        } else{
            Log.e("ERROR", "SOME OF YOUR INTENT IS NULL");
        }
    }

@Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        if (location != null) {
            mLastLocation = location;
            double latitude = mLastLocation.getLatitude();
            double longitude = mLastLocation.getLongitude();
            Log.i("info", "Latitude :: " + latitude);
            Log.i("info", "Longitude :: " + longitude);
            //sending location details
            Log.e("lochabged", pass);
            sendLocation(dr, String.valueOf(latitude), String.valueOf(longitude), pa); <--here pa and dr are not null
        }
    }

我在IntentService中也得到了W / System.err:android.os.NetworkOnMainThreadException。它不在工作线程上运行吗?这个类以前是一个服务,我删除它重命名它并清理我的项目,但我仍然得到相同的错误。

10-09 13:42:16.317 30850-30850/mypackage  W/System.err: android.os.NetworkOnMainThreadException
10-09 13:42:16.320 30850-30850/mypackage  W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
10-09 13:42:16.321 30850-30850/mypackage  W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-09 13:42:16.321 30850-30850/mypackage W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-09 13:42:16.322 30850-30850/mypackage  W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:112)
10-09 13:42:16.322 30850-30850/mypackage  W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-09 13:42:16.322 30850-30850/mypackage W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
10-09 13:42:16.322 30850-30850/mypackage  W/System.err:     at java.net.Socket.connect(Socket.java:833)
10-09 13:42:16.323 30850-30850/mypackage W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131)
10-09 13:42:16.323 30850-30850/mypackage W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:101)
10-09 13:42:16.323 30850-30850/mypackage  W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
10-09 13:42:16.323 30850-30850/mypackage  W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
10-09 13:42:16.325 30850-30850/mypackage  W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
10-09 13:42:16.326 30850-30850/mypackage  W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
10-09 13:42:16.326 30850-30850/mypackage  W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
10-09 13:42:16.327 30850-30850/mypackage  W/System.err:     at mypackage .InitialLocationUpdater.sendLocation(InitialLocationUpdater.java:136)
10-09 13:42:16.327 30850-30850/mypackage  W/System.err:     at mypackage .InitialLocationUpdater.onLocationChanged(InitialLocationUpdater.java:118)
10-09 13:42:16.328 30850-30850/mypackage  W/System.err:     at com.google.android.gms.location.internal.zzk$zzb.handleMessage(Unknown Source)
10-09 13:42:16.329 30850-30850/mypackage  W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-09 13:42:16.329 30850-30850/mypackage  W/System.err:     at android.os.Looper.loop(Looper.java:136)
10-09 13:42:16.329 30850-30850/mypackage  W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5086)
10-09 13:42:16.329 30850-30850/mypackage  W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 13:42:16.330 30850-30850/mypackage  W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
10-09 13:42:16.330 30850-30850/mypackage  W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-09 13:42:16.331 30850-30850/mypackage W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-09 13:42:16.331 30850-30850/mypackage  W/System.err:     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案