2011-03-06 89 views
0

我的問題是類似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

回答

0

嘗試放置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

-1

忘記所有使用這個簡單的和良好的工作代碼和樂趣:)

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