Android位置管理器在调用时崩溃应用程序

时间:2016-09-19 13:46:32

标签: android locationmanager locationlistener

每次用户点击按钮时,我都会编写一个代码来获取设备的当前位置。但是当我打电话给听众时,应用程序就崩溃了。谁能让我了解我做错了什么?提前谢谢。

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) 

1 个答案:

答案 0 :(得分:1)

改为使用服务addListenerLocation();方法中的onCreate()