2013-09-22 52 views
1

我的應用出現問題。我的應用實際上是一項服務。我有主擴展服務,它有私人Looper looper變量來獲取HandlerThread活套。在onCreate函數中,我初始化了位置管理器,位置監聽器和HandlerThread(將其looper設置爲looper變量),然後嘗試使用傳入looper變量的requestLocationUpdates作爲looper。我得到一個錯誤Android應用崩潰(looper = null)

09-22 17:30:24.069: E/AndroidRuntime(1414): Caused by: java.lang.IllegalArgumentException: looper==null 

我應該爲這個HandlerThread做些什麼嗎?也許開始吧?:>

我不粘貼任何代碼,因爲它很長,我不知道哪個相關部分適合解決問題。因此,我很想通過任何代碼您可能需要(HandlerThread?還有什麼?)

感謝您的任何幫助。

** EDIT * *

好的,的onCreate功能:

public void onCreate() { 
    super.onCreate(); 
    Log.d("Service", "Service onCreate starts"); 
    running = true; 
    lt = new LooperThread("GPSIntentLT"); 
    serviceLocationM = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    serviceLocationL = new MyLocationListener(); 

    requestUpdates(serviceLocationL); 
    Log.d("Service", "Service onCreate ends"); 
} 

requestUpdates函數(上面稱爲,出現錯誤):

private void requestUpdates(LocationListener listener) 
{ 
    Log.d("Service", "requestUpdates starts"); 
    serviceLocationM.removeUpdates(listener); 
    flag = displayGpsStatus(); 
    switch(flag) 
    { 
    case 0: 
     Log.d("Service", "No Location Provider"); 
     break; 
    case 1: 
     Log.d("Service", "Network Provider"); 
     serviceLocationM.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 25, listener, theLooper); 
     break; 
    case 2: 
     Log.d("Service", "GPS Provider"); 
     serviceLocationM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 25, listener, theLooper); 
     break; 
    } 
    Log.d("Service", "requestUpdates ends"); 
} 

而HandlerThread:

private class LooperThread extends HandlerThread{ 

    public LooperThread(String name) { 
     super(name); 
     Log.d("Service", "LooperThread constructor starts"); 
     theLooper = getLooper(); 
     Log.d("Service", "LooperThread constructor ends"); 
    } 

    @Override 
    public void run() { 
     super.run(); 
     Log.d("Service", "LooperThread run called"); 
    } 

} 

和結束時,logcat的爲這個應用程序:

09-22 18:21:47.997: D/Service(386): Service onCreate starts 
09-22 18:21:47.997: D/Service(386): LooperThread constructor starts 
09-22 18:21:48.007: D/Service(386): LooperThread constructor ends 

所以它不下去的requestLocationUpdates功能,它發生在2.2模擬器,在2.3.3崩潰了整個模擬器通過殺死它的進程(?)。

+0

您至少應該發佈類,onCreate和處理程序線程的代碼。 – Jems

+0

@Jems添加了代碼。 – Ziker

+0

爲什麼要擴展HandlerThread? – pskink

回答

2

來源:http://developer.android.com/reference/android/os/HandlerThread.html#getLooper%28%29

此方法返回與此線程關聯的活套。如果此線程未啓動或出於任何原因isAlive()返回false,則此方法將返回null。如果這個線程已經啓動,這個方法會阻塞,直到活套已經初始化。

由於您在LooperThread的構造函數中調用此方法,當然您還沒有啓動它(通過調用start())。 該方法返回null,它本身是有效的,並且構造函數完成,但是隨後在requestUpdates()中傳遞null,從而導致崩潰。

您將需要啓動線程,然後獲取對活套的引用。在嘗試以另一種方法使用它之前,請確保它已準備就緒。

+0

好吧,似乎合理,我做了你所說的 - 我得到了錯誤信息 '每個線程只能創建一個活套「# 我沒有創建任何活套,只是初始化了這個句柄並得到它的活套。我會補充說我初始化它後有lt.run(),但它永遠不會進入該函數。 – Ziker

+0

這聽起來像是一個單獨的問題,搜索本網站上的錯誤。我更正了我的答案,使用start()而不是run()。 – Jems

+1

好吧,'start()'而不是'run()'修正了錯誤。它也工作(獲取位置)。你可能會告訴我爲什麼函數'onStatusChanged'在我的應用程序使用'onLocationChanged'時在我的LocationListener上被調用?這是正常的嗎?我以爲它被稱爲只有當供應商改變其狀態:> – Ziker