我的應用出現問題。我的應用實際上是一項服務。我有主擴展服務,它有私人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崩潰了整個模擬器通過殺死它的進程(?)。
您至少應該發佈類,onCreate和處理程序線程的代碼。 – Jems
@Jems添加了代碼。 – Ziker
爲什麼要擴展HandlerThread? – pskink