2016-11-08 91 views
2

有時我面臨這個問題,我的意圖服務崩潰,而我再次onReceive()方法重新啓動它。 這是我的堆棧跟蹤。一些時間意圖服務崩潰在啓動服務再次響應Reciver

「顯示java.lang.NullPointerException:嘗試在android.app.ContextImpl.validateServiceIntent上的空對象引用 調用虛擬方法android.content.ComponentName android.content.Intent.getComponent()'(ContextImpl .java:1207) at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1238) at android.app.ContextImpl.startService(ContextImpl.java:1222) at android.content.ContextWrapper.startService(ContextWrapper.java :581) at com.live.wheelz.MapFragmentPassenger $ ResponseReceiver $ 3.run(MapFragmentPassenger.java:3162) at android.os.Handler.handleCallback(Handler.java:739) a t android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java .lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit。 Java的:616) 「 這裏是我的代碼

public class ResponseReceiver extends WakefulBroadcastReceiver { 
     public static final String ACTION_RESP = "com.live.ResponseReceiver.intent.action.MESSAGE_PROCESSED"; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if(!intent.getStringExtra("journeyState").equals("jr_droped") && !intent.getStringExtra("journeyState").equals("jr_canceled")) { 
       if(intent.getStringExtra("journeyState").equals("jr_arrived") && alertCounter==0) { 
        alertCounter=alertCounter+1; 
        popUp.dismisDialog(); 
        soundAlert.initRigtone("Driver is arrived at your location"); 
//     snack("Driver is arrived at your location","message","long"); 


       } 
       if (sharedData.getisSimulation() == true) { 
        if (intent.getBooleanExtra("same", true) == false) { 
         double latc = intent.getDoubleExtra("pDriverLatitude", 0.0); 
         double lonc = intent.getDoubleExtra("pDriverLongitude", 0.0); 
         //dname.setText("Latitude:"+lat+"Longitude:"+lon); 
         if (driver != null) { 
          driver.remove(); 
         } 
         //String address=getaddress(latpickup,lonpickup); 
         for (Marker oc : nearest_driver) { 
          oc.remove(); 
         } 
         driver = googleMap.addMarker(new MarkerOptions() 
           .position(new LatLng(latc, lonc)) 
           .title("Current location Driver").icon(BitmapDescriptorFactory.fromResource(R.drawable.bike_left)) 
           .draggable(true)); 
         if (markerlocation != null) { 
          markerlocation.remove(); 

         } 
         markerlocation = googleMap.addMarker(new MarkerOptions() 
           .position(new LatLng(sharedData.getnewlat(), sharedData.getnewlon())) 
           .title("Passenger Current Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.bluedot))); 
         if (sharedData.getisRating() == false) { 
          Handler handler = new Handler(); 
          handler.postDelayed(new Runnable() { 
           @Override 
           public void run() { 
            try { 
             startService(servicedriver); 
            } 
            catch (Exception ex) 
            { 
             System.out.print(""); 
            } 
           } 
          }, sharedData.getPassenger_request_delay_time()); 
         } 

        } 
        if (intent.getBooleanExtra("same", false) == true) { 
         if (markerlocation != null) { 
          markerlocation.remove(); 

         } 
         markerlocation = googleMap.addMarker(new MarkerOptions() 
           .position(new LatLng(sharedData.getnewlat(), sharedData.getnewlon())) 
           .title("Passenger Current Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.bluedot))); 
         if (sharedData.getisRating() == false) { 
          if (receiver != null) { 
           Handler handler = new Handler(); 
           handler.postDelayed(new Runnable() { 
            @Override 
            public void run() { 
             try { 
              startService(servicedriver); 
             } 
             catch (Exception ex) 
             { 
              System.out.print(""); 
             } 
             //startService(servicedriver); 
            } 
           }, sharedData.getPassenger_request_delay_time()); 
          } 
         } 
        } 
       } 
       if (sharedData.getisSimulation() == false) { 

        if (intent.getBooleanExtra("same", false) == false) { 
         if (intent.getBooleanExtra("connection_error", false) == false) { 
          latc = intent.getDoubleExtra("pDriverLatitude", 0.0); 
          lonc = intent.getDoubleExtra("pDriverLongitude", 0.0); 
          //snack("Driver Current lat:"+latc+" Driver Current lon:"+lonc, "message", " "); 
          //Ali Abbas Location Diffrnce Calc 
          double latDrvr = sharedData.getPickuplocationlat(); 
          double lonDrvr = sharedData.getPickuplocationlon(); 
          double distanceDiff = getDistanceInMiles(latc,lonc,latDrvr,lonDrvr); 
          double getDisrnce = sharedata.getDrvrPsngrRnge() ; 
          if(distanceDiff <= sharedata.getDrvrPsngrRnge() && intent.getStringExtra("journeyState").equals("jr_driverAccepted")) { 
           /*if (!ISPOPUPED) { 
            ISPOPUPED =true ;*/ 
            fncDrvrArndPsngr(); 
           //} 
          }else if(distanceDiff > sharedata.getDrvrPsngrRnge() && intent.getStringExtra("journeyState").equals("jr_driverAccepted")){ 
           fncDrvrArndPsngrPopUpDismiss(); 
          } 
          //Ali Abbas Distance Calc Ends 
         } 
         smoothly(latc, lonc); 

         if (sharedData.getisRating() == false) { 
          if (intent.getBooleanExtra("connection_error", true) == true) { 
           snack("Internet Connection Error", "message", " "); 
           fncDrvrArndPsngrPopUpDismiss(); 
          } else { 
           AppAsynTaskAddress setaddress = new AppAsynTaskAddress(MapFragmentPassenger.this, latc, lonc, "driverlocation", false); 
           setaddress.execute(); 
           //snack("Driver Arrived at:"+pickuplocationtext.getText().toString(), "message", " "); 
          } 
          if (receiver != null) { 
           Handler handler = new Handler(); 
           handler.postDelayed(new Runnable() { 
            @Override 
            public void run() { 
             try { 
              startService(servicedriver); 
             } 
             catch (Exception ex) 
             { 
              System.out.print(""); 
             } 
             //startService(servicedriver); 
            } 
           }, sharedData.getPassenger_request_delay_time());//sharedData.getPassenger_request_delay_time() 
          } 
         } else { 
          googleMap.clear(); 

         } 


        } 
        if (intent.getBooleanExtra("same", true) == true) { 
         if (sharedData.getisRating() == false) { 
          //snack("Same lat lon", "message", " "); 
          if (receiver != null) { 
           Handler handler = new Handler(); 
           handler.postDelayed(new Runnable() { 
            @Override 
            public void run() { 
             try { 
              startService(servicedriver); 
             } 
             catch (Exception ex) 
             { 
              System.out.print(""); 
             } 
             // startService(servicedriver); 
            } 
           }, sharedData.getPassenger_request_delay_time());//sharedData.getPassenger_request_delay_time() 
          } 
         } else { 
          googleMap.clear(); 

         } 

        } 
       } 
      } 
      if(intent.getStringExtra("journeyState").equals("jr_droped")) { 
       if(!sharedata.isOnRatingScreen()) { 
        Intent intSetPass = new Intent(MapFragmentPassenger.this, Ratingscreenpassenger.class); 
        intSetPass.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(intSetPass); 
       } 

      } 

      if(intent.getStringExtra("journeyState").equals("jr_canceled")) { 
       sharedata.setJmode(false); 
       sharedata.setisRating(true); 
       //db.deleteBillingParameter(); 
       db.deleteJourneyRating(sharedata.getDriver_rquested_journeyid(), "p"); 
       changebutton(); 
       snack("Journey Canceled","message","long"); 
       soundAlert.initRigtone("Journey Canceled"); 
      } 

      } 


     } 
+0

的可能的複製(HTTP [什麼是空指針異常,以及如何解決它?] //stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – 0X0nosugar

回答

3

我解決這個問題做一些調試startService(servicedriver)之後; servicedriver對象爲空我再次啓動它,如果它是空喜歡

if(servicedriver!=null) { 
              startService(servicedriver); 
             } 
             else{ 
              startservice(); 
             } 

這裏是啓動服務

public void startservice() { 
    if (sharedData.getisSimulation() == true) { 
     jmode = true; 
     sharedData.setisRating(false); 
     servicedriver = new Intent(this, LocationService.class); 
     this.startService(servicedriver); 
     sharedData.setJmode(true); 
    } 
    if (sharedData.getisSimulation() == false) { 
     jmode = true; 
     sharedData.setisRating(false); 
     servicedriver = new Intent(this, LocationServiceDriver.class); 
     this.startService(servicedriver); 
     sharedData.setJmode(true); 
    } 
}