调用getSystemService(LOCATION_SERVICE)时Android应用程序崩溃

时间:2011-03-06 21:40:19

标签: java android eclipse

我的问题类似于Android starting application after searching for it

我打电话

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

来自我的主要活动onCreate()。 我的LocationHelper类看起来像这样:

public class LocationHelper {

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

当我离开setLocationManager()呼叫时,该应用程序运行正常,如果不是它崩溃并留下堆栈:

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]

有人可以帮我解释这些消息,因为我在调试Android应用程序时没有找到任何文档。

2 个答案:

答案 0 :(得分:0)

尝试使用context.getSystemService(Context.LOCATION_SERVICE);直接进入onCreate-Method。它在那里有用吗?

您是否已请求Manifest.xml中的权限访问位置?

答案 1 :(得分:-1)

忘记所有使用这个简单而好的工作代码并享受乐趣:)

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

不要使用

context.getSystemService(Context.LOCATION_SERVICE)

 this.getSystemService(Context.LOCATION_SERVICE)