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

问题描述:

我的问题是类似Android starting application after searching for it的Android应用程序崩溃调用getSystemService(LOCATION_SERVICE)时

我打电话

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应用程序的任何地方找到任何文档。

+0

日Thnx的清理,火狐4B12没有显示编辑器的所有选项... – Julian 2011-03-06 21:50:35

+0

Kesssel:这不是一个堆栈跟踪。或者,更准确地说,这是Eclipse对错误堆栈跟踪的再现。运行过去的异常点并使用DDMS透视图检查LogCat以查找真正的异常。 – CommonsWare 2011-03-06 23:02:54

+0

我的问题是,因为我的调用在'onCreate()'中,程序在开始时崩溃了。因此我无法激活堆检查器或线程查看器。 – Julian 2011-03-07 17:16:25

尝试放置context.getSystemService(Context.LOCATION_SERVICE);直接进入onCreate-Method。它在那里工作吗?

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

+0

我注册了ACCESS_MOCK_LOCATION; ACCESS_FINE_LOCATION;清单中的ACCESS_COARSE_LOCATION;传递LocationManager直接导致相同的错误。 – Julian 2011-03-06 22:15:27

忘记所有使用这个简单的和良好的工作代码和乐趣:)

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

不要使用

context.getSystemService(Context.LOCATION_SERVICE) 

this.getSystemService(Context.LOCATION_SERVICE) 
+0

这是错误的,如果我们不在Activity类中,我们需要获取对实际上下文的引用,在我的情况下,问题在于我对无效上下文引用调用了getSystemService,现在我的getContext()方法被调用。 getSystemService – Matteo 2016-09-20 08:21:34