我的应用出现问题。我的应用实际上是一项服务。我有主扩展服务,它有私人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