简体   繁体   中英

Android app crashes when invoking getSystemService(LOCATION_SERVICE)

My problem is similar to Android starting application after searching for it

I call

locationHelper.setLocationManager(this);  // pass the appcontext to my helper method

from my main Activity's onCreate() . My LocationHelper class looks like this:

public class LocationHelper {

  private LocationManager locationManager;
    [...]
    public void setLocationManager(Context context){
      locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }
    [...]
}

When I leave the setLocationManager() call out, the app runs fine, if not it crashes and leaves following on the Stack:

Thread [<1> main] (Suspended (exception RuntimeException))  
    ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1647    
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1663 
    ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 117   
    ActivityThread$H.handleMessage(Message) line: 931   
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 3683    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 507  
    ZygoteInit$MethodAndArgsCaller.run() line: 839  
    ZygoteInit.main(String[]) line: 597 
    NativeStart.main(String[]) line: not available [native method]

Could someone help me interpret those messages since I haven't found any documentation anywhere on debugging android apps.

Try putting the context.getSystemService(Context.LOCATION_SERVICE); directly into the onCreate-Method. Does it work there?

Have you requested the permissions in your Manifest.xml to access Locations?

Forget all use this simple and good working code and have fun :)

LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

Don't use

context.getSystemService(Context.LOCATION_SERVICE)

or

 this.getSystemService(Context.LOCATION_SERVICE) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM