2016-09-19 133 views
1

我編寫了一個代碼,用於在用戶每次單擊按鈕時獲取設備的當前位置。但是當我打電話給聽衆時,應用程序就會崩潰。任何人都可以讓我瞭解我做錯了什麼嗎?提前致謝。Android位置管理器在調用時崩潰應用程序

public void onStartService() 
{ 
    addListenerLocation(); 
} 

@SuppressLint("NewApi") 
private void addListenerLocation() 
{ 
    mLocationManager = (LocationManager) 
      getSystemService(Context.LOCATION_SERVICE); 
    LocationListener mLocationListener = new LocationListener() 
    { 

     public void onLocationChanged(Location location) 
     { 
      currentLat = location.getLatitude(); 
      InstanciaObjetos.setPEDLAT(currentLat); 
      currentLon = location.getLongitude(); 
      InstanciaObjetos.setPEDLON(currentLon); 
     } 
     public void onStatusChanged(String provider, int status, Bundle extras) 
     { 
      //TODO -- Barney says what should happen 
     } 
     public void onProviderEnabled(String provider) 
     { 
      Location lastKnownLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      if(lastKnownLocation!=null) 
      { 
       currentLat = lastKnownLocation.getLatitude(); 
       currentLon = lastKnownLocation.getLongitude(); 
      } 

     } 
     public void onProviderDisabled(String provider) 
     { 

     } 
    }; 
    mLocationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 0, 0, mLocationListener); 
} 

啓動服務我只是調用onStartService()。還有什麼我需要做的?

我剛剛附加了logcat。現在我意識到需要做什麼(我猜)。

09-21 23:17:41.209 8861-8861/br.com.appvendasfs E/AndroidRuntime: FATAL EXCEPTION: main 
Process: br.com.appvendasfs, PID: 8861 
java.lang.IllegalStateException: Could not execute method for android:onClick 
    at android.view.View$DeclaredOnClickListener.onClick(View.java:4452) 
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at 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) 
Caused by: java.lang.reflect.InvocationTargetException 
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at 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) 
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate() 
    at android.app.Activity.getSystemService(Activity.java:5253) 
    at appVendasFS.DAO.PedidosDAO.addListenerLocation(PedidosDAO.java:47) 
    at appVendasFS.DAO.PedidosDAO.onStartService(PedidosDAO.java:40) 
    at appVendasFS.DAO.PedidosDAO.Inserir(PedidosDAO.java:97) 
    at appVendasFS.UI.TelaVenda.Inserir_Pedido(TelaVenda.java:813) 
    at appVendasFS.UI.TelaVenda.onClick_botao(TelaVenda.java:593) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at 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)  
+0

請添加崩潰報告 – Saveen

+0

http://s2.quickmeme.com/img/94/94eb289f247f6e711a9975bed6783d1db4a15af4a9f2b7cd8f1a560a3bfb540a.jpg; d – jdstaerk

回答

1

呼叫addListenerLocation();在服務的onCreate()方法來代替。

+0

我把它稱爲上「的onCreate()方法和它的工作。謝謝你,先生。 –

+0

不客氣!謝謝你1 :) – whitebrow