android - 尝试从 Google Cloud Messaging 消息更改 Google Maps v2 Api 上的标记时出现非法状态异常

标签 android maps google-cloud-messaging illegalstateexception

我正在编写一个 Android 应用程序,我在 Google map 上有标记,应该更改为 GCM 接收到的位置。 我有一个全局静态列表,其中我将标记与 id 和位置一起保存。 当我收到一条消息时,我可以更改对象在我的 GCMBaseIntentService 列表中的位置,但是当我想执行以下代码时:

    mMarker.setPosition(new LatLng(member.getLatitude(),member.getLongitude()));

我得到以下异常:

    01-13 18:52:38.118: E/AndroidRuntime(7605): FATAL EXCEPTION: IntentService[GCMIntentService-1041237551356-1]
    01-13 18:52:38.118: E/AndroidRuntime(7605): java.lang.IllegalStateException: Not on the main thread
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at maps.am.r.b(Unknown Source)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at maps.ar.d.b(Unknown Source)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at maps.y.ae.addMarker(Unknown Source)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:167)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at android.os.Binder.transact(Binder.java:279)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at com.example.MapViewActivity.showMember(MapViewActivity.java:529)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at com.example.MapViewActivity.updateMember(MapViewActivity.java:417)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at com.example.MapViewActivity.updateMembers(MapViewActivity.java:410)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at com.example.pushnotifications.GCMIntentService.onMessage(GCMIntentService.java:75)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at android.os.Handler.dispatchMessage(Handler.java:99)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at android.os.Looper.loop(Looper.java:130)
    01-13 18:52:38.118: E/AndroidRuntime(7605):     at android.os.HandlerThread.run(HandlerThread.java:60)

我尝试在 map 上添加一个按钮来刷新所有标记(使用完全相同的代码)并且效果很好。 所以不知何故,问题是 GCM 是在不同的线程或类似的东西中执行的,而且我似乎无法更改来自不同线程的标记...

我尝试了一个丑陋的解决方案:每秒刷新所有标记。 但是从 MapViewActivity 创建线程会给出相同的异常:

private Thread MarkerUpdater = new Thread(
    new Runnable() {
        public void run() {
            while(true) {
                updateMembers();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });

也许有人知道一个不错的解决方案会是什么样子?

最佳答案

您需要获取主 UI 线程的处理程序并在其中发布可运行对象。

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable(){ 
   // your UI code here 
}

您在 Runnable 中从您的服务引用的任何变量都需要是最终的。既然您说过要将数据共享为全局/静态,那么您应该没问题。

关于android - 尝试从 Google Cloud Messaging 消息更改 Google Maps v2 Api 上的标记时出现非法状态异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14308789/

相关文章:

php - Android 错误 : GCM using PHP

android - 单击时使用 TextView 扩展 cardView

android - 在虚拟机上运行Android进行开发

Android WebView VS 手机浏览器

android - 谷歌 API key 丢失

ruby - 您将如何使用 Ruby 显示 2D map ?

java - 如何在 Android widget "ScrollView"中编辑滚动条的高度?

javascript - OpenLayers 3 Vector 缩放错误位置

google-cloud-messaging - 应用程序应何时在 GCM 中请求实例 ID 和注册 token ?

javascript - 谷歌浏览器推送通知