我正在尝试从FragmentActivity Map中的Goolge地图中删除标记,如果表中没有可用于服务器端的制造商的数据,那么数据对象也是空的,但我收到以下错误。我该如何解决?
错误:
07-12 20:53:05.697: E/AndroidRuntime(26364): FATAL EXCEPTION: IntentService[IntentService]
07-12 20:53:05.697: E/AndroidRuntime(26364): Process: com.bustracker, PID: 26364
07-12 20:53:05.697: E/AndroidRuntime(26364): java.lang.IllegalStateException: Not on the main thread
07-12 20:53:05.697: E/AndroidRuntime(26364): at com.google.l.a.ce.b(Unknown Source)
07-12 20:53:05.697: E/AndroidRuntime(26364): at com.google.maps.api.android.lib6.d.ct.a(Unknown Source)
07-12 20:53:05.697: E/AndroidRuntime(26364): at com.google.maps.api.android.lib6.d.aq.a(Unknown Source)
07-12 20:53:05.697: E/AndroidRuntime(26364): at com.google.android.gms.maps.model.internal.t.onTransact(SourceFile:51)
07-12 20:53:05.697: E/AndroidRuntime(26364): at android.os.Binder.transact(Binder.java:380)
07-12 20:53:05.697: E/AndroidRuntime(26364): at com.google.android.gms.maps.model.internal.zzi$zza$zza.remove(Unknown Source)
07-12 20:53:05.697: E/AndroidRuntime(26364): at com.google.android.gms.maps.model.Marker.remove(Unknown Source)
07-12 20:53:05.697: E/AndroidRuntime(26364): at com.bustracker.GetLLRD.onHandleIntent(GetLLRD.java:120)
07-12 20:53:05.697: E/AndroidRuntime(26364): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
07-12 20:53:05.697: E/AndroidRuntime(26364): at android.os.Handler.dispatchMessage(Handler.java:102)
07-12 20:53:05.697: E/AndroidRuntime(26364): at android.os.Looper.loop(Looper.java:145)
07-12 20:53:05.697: E/AndroidRuntime(26364): at android.os.HandlerThread.run(HandlerThread.java:61)
GetLLRD IntentService类中的onHandleIntent方法:
protected void onHandleIntent(Intent intent) {
if (data != null && !data.isEmpty()) {
Intent intent1 = new Intent(this, Map.class);
intent1.putExtra("list_data", data);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent1);
} else if (Map.markerMap != null
&& !Map.markerMap.isEmpty()) {
Iterator<HashMap.Entry<Integer, Marker>> it = Map.markerMap
.entrySet().iterator();
while (it.hasNext()) {
HashMap.Entry<Integer, Marker> entery = it
.next();
int key = entery.getKey();
Map.marker = Map.markerMap.get(key);
System.out.println("test marker " + Map.marker );
//Line 120.
Map.marker .remove();
Map.markerMap.remove(key);
// Marker value = entery.getValue();
}
}
映射fragmentActivity:
public class Map extends FragmentActivity implements OnMapReadyCallback {
GoogleMap map;
static HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();
static Marker marker = null;
...
}
答案 0 :(得分:2)
异常的快速修复是将地图更新放在Runnable
中以在主线程上执行(下面的代码)。但是,问题的真正答案是你需要重新思考你的设计。 IntentService
中的代码在活动中使用静态数据是不安全的。使用IntentService
的原因是什么?地图更新必须在主线程上运行。 IntentService
的目的之一是执行无法在主线程上完成的操作,并且需要后台线程。为什么在包含需要主线程的代码时使用IntentService
?
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Iterator<HashMap.Entry<Integer, Marker>> it = Map.markerMap
.entrySet().iterator();
while (it.hasNext()) {
HashMap.Entry<Integer, Marker> entery = it
.next();
int key = entery.getKey();
Map.marker = Map.markerMap.get(key);
System.out.println("test marker " + Map.marker );
//Line 120.
Map.marker.remove();
// Remove from map using iterator
it.remove();
// Marker value = entery.getValue();
}
}
});
答案 1 :(得分:0)
您无法从后台线程启动活动。 onHandleIntent
中的代码在后台线程中执行。要从主线程启动活动,您需要将runnable发布到主线程looper。
protected void onHandleIntent(Intent intent) {
if (data != null && !data.isEmpty()) {
Handler mainHandler = new Handler(getApplicationContext.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
Intent intent1 = new Intent(this, Map.class);
intent1.putExtra("list_data", data);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent1);}
});
} else if (Map.markerMap != null && !Map.markerMap.isEmpty()) {..........}
}