gpt4 book ai didi

android - LocationManager:java.lang.RuntimeException:无法在尚未调用 Looper.prepare() 的线程内创建处理程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:07 25 4
gpt4 key购买 nike

我认为从另一个线程在 UI 线程上执行某些操作时通常会出错,但我不明白自己做错了什么。该错误似乎只在手机旅行时出现,因此 GPS 位置不断变化。

我想存储最近的位置,所以 UI 上没有任何内容。我从主要 Activity 中调用了以下方法:

    public void getFreshDeviceLocation(long interval, final long maxtime) {
if (gps_recorder_running){return;}
gpsTimer = new Timer();
//starts location
startMillis=System.currentTimeMillis();

// receive updates
for (String s : locationManager.getAllProviders()) {

LocationListener listener=new LocationListener() {


@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onLocationChanged(Location location) {
// if this is a gps location, we can use it
if (location.getProvider().equals(
LocationManager.GPS_PROVIDER)) {
doLocationUpdate(location, true); //stores the location in the prefs
stopGPS();
}
}

@Override
public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub

}
};

locationManager.requestLocationUpdates(s, interval, //line 97, error!
minDistance, listener);
myListeners.add(listener);

gps_recorder_running = true;
}

// start the gps receiver thread
gpsTimer.scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {
Location location = getBestLocation();
doLocationUpdate(location, false);
if ((System.currentTimeMillis()-startMillis)>maxtime){if (maxtime>0){stopGPS();}}
//Updates depend on speed of the device
float speed=pref.DeviceLocation().getSpeed();
if (speed<1){if (speedclass!=0){speedclass=0;stopGPS();getFreshDeviceLocation(300000,0);}}
if ((speed>=1)&&(speed<3)){if (speedclass!=1){speedclass=1;stopGPS();getFreshDeviceLocation(90000,0);}}
if ((speed>=3)&&(speed<17)){if (speedclass!=2){speedclass=2;stopGPS();getFreshDeviceLocation(15000,0);}}
if (speed>=17){if (speedclass!=3){speedclass=3;stopGPS();getFreshDeviceLocation(10000,0);}}
}
}, 0, interval);

}

我得到的错误是:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:139)
at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:137)
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:708)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:630)
at com.appiclife.tmoflashlight.TMO_LocationManager.getFreshDeviceLocation(TMO_LocationManager.java:97)
at com.appiclife.tmoflashlight.TMO_LocationManager$2.run(TMO_LocationManager.java:116)
at java.util.Timer$TimerImpl.run(Timer.java:289)

第 97 行=locationManager.requestLocationUpdates(s, interval,minDistance, listener); (位置管理器类)

好像我必须调用 Looper.prepare();和 Looper.loop();某处,但我看不到在哪里?可能跟这个问题有关AsyncTask and Looper.prepare() error

最佳答案

在这种情况下,我认为你需要的是一个循环线程,this可能会帮助你。

或者您可以在 run() 方法中创建一个处理程序,使其成为循环线程,如下例所示。

Handler mHandler;

public void run(){
Looper.prepare();
mHandler = new Handler(){
public void handleMessage(Message msg){
Looper.myLooper().quit();
}
};
//do your stuff
//...
mHandler.sendEmptyMessage(0); //send ourself a message so the looper can stop itself
Looper.loop();
}//end of run

如果有帮助,请告诉我:)

关于android - LocationManager:java.lang.RuntimeException:无法在尚未调用 Looper.prepare() 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16600349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com