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

android – 尝试从Google Cloud Messaging消息更改Google Maps v2 Api上的标记时的非法状态异常,第1张

概述我正在编写一个 Android应用程序,我在Google地图上有标记,应该更改为我通过GCM收到的位置. 我有一个全局静态列表,我将标记与id和位置一起保存. 当我收到消息时,我可以在GCMBaseIntentService中更改列表中对象的位置,但是当我想执行以下代码时: mMarker.setPosition(new LatLng(member.getLatitude(),member.get 我正在编写一个 Android应用程序,我在Google地图上有标记,应该更改为我通过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)

尝试在地图上添加一个按钮来刷新所有标记(具有完全相同的代码),并且工作正常.
所以不知何故问题是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线程的处理程序并在那里发布runnable.
Handler handler = new Handler(Looper.getMainLooper());handler.post(new Runnable(){    // your UI code here }

您在Runnable中从服务中引用的任何变量都必须是最终的.既然你说你正在以全局/静态方式共享数据,你应该没问题.

总结

以上是内存溢出为你收集整理的android – 尝试从Google Cloud Messaging消息更改Google Maps v2 Api上的标记时的非法状态异常全部内容,希望文章能够帮你解决android – 尝试从Google Cloud Messaging消息更改Google Maps v2 Api上的标记时的非法状态异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1134015.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存