gpt4 book ai didi

java - 在 Android 中的单独线程上使用循环器

转载 作者:行者123 更新时间:2023-12-01 11:40:31 24 4
gpt4 key购买 nike

背景:

我试图通过 handler.postDelayed 逐步移动标记来显示公交车的移动,直到它从一站到达下一站。

我希望它在一段时间后重复下一站,因此我尝试在单独的线程上使用循环器,因为这对于主 UI 线程来说工作量太大。

问题:

因为我正在更新标记的位置,所以我需要每秒为其设置一个新位置,但是在运行代码时,我遇到错误,指出它不在主 UI 线程上(请参阅底部这篇文章)。

错误指向存储busMarker的变量,我认为该变量只能由创建它的线程修改。

我已经尝试过 runOnUiThread() 但我仍然会收到其他错误,例如空值,这不应该是因为我给它们分配了值,但仅在主线程中。

我假设有一种比必须不断返回主线程更干净的方法,那么我该如何实现这一点?

创建线程

private class ThreadClass extends Thread {

@Override
public void run() {

Looper.prepare();

moveBusMarker();

if (passedStops.size() != stops.size()) {

Looper.loop();
}
else {

Looper.myLooper().quit();
}
}
}

运行线程

    if (passedStops.size() != 0 && passedStops.size() != stops.size()) {
thread.start();
}

执行运动

// set up a timer
final long limit = TimeUnit.SECONDS.toMillis(seconds) - 1000;
final long startTime = System.currentTimeMillis();

final Stop NextStop = nextStop;

final Handler handler1 = new Handler();

Runnable runnable = new Runnable() {

@Override
public void run() {

Log.d("", "The bus is currently at " + busPosition.toString());

// get the current bus' position
double lat = busPosition.latitude;
double lon = busPosition.longitude;

// add the difference to the bus position to move it closer
lat = lat + latDifference;
lon = lon + lonDifference;
busPosition = new LatLng(lat, lon);

Log.d("", "The bus has moved to " + busPosition.toString());

// set the new position to the marker representing the bus movement
busMarker.setPosition(busPosition);


// it hasn't reached the next stop, continue to animate
if ((System.currentTimeMillis() - startTime) < limit) {
handler1.postDelayed(this, 1000);
}

// else the time is up i.e. the bus has reached the next stop, so set the new target
else {
Log.d("", "The bus has passed " + NextStop.getName());

passedStops.add(NextStop);
Log.d("", passedStops.toString());

createPolyline();
}
}

};

handler1.post(runnable);

记录的错误

Process: com.example.sanj.fyp, PID: 18904
java.lang.IllegalStateException: Not on the main thread
at com.google.l.a.cd.b(Unknown Source)
at com.google.maps.api.android.lib6.c.ca.a(Unknown Source)
at com.google.maps.api.android.lib6.c.aj.a(Unknown Source)
at com.google.android.gms.maps.model.internal.t.onTransact(SourceFile:73)
at android.os.Binder.transact(Binder.java:361)
at com.google.android.gms.maps.model.internal.l$a$a.setPosition(Unknown Source)
at com.google.android.gms.maps.model.Marker.setPosition(Unknown Source)
at com.example.sanj.fyp.main.fragment.LiveServiceFragment$2$1.run(LiveServiceFragment.java:423)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at com.example.sanj.fyp.main.fragment.LiveServiceFragment$ThreadClass.run(LiveServiceFragment.java:115)

最佳答案

无法确定您的 handler1 变量是从哪个线程创建的。确保它位于 UI 线程上。或者快速修复:

handler1 = new Handler(context.getApplicationContext().getMainLooper());

关于java - 在 Android 中的单独线程上使用循环器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29561178/

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