illegal state exception when trying to change a marker on a Google Maps v2 Api from a Google Cloud Messaging message

青春壹個敷衍的年華 提交于 2019-12-05 07:17:42

You need to get the handler for your main UI thread and post a runnable there.

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

Any variables you reference from your service in your Runnable will need to be final. Since you said you are sharing the data as global/static, you should be okay.

Kenneth Saey

This might be useful for someone else: I just had a very similar problem of markers not updating when receiving a GCM message. The solution is indeed to put the marker update code in a main loop handler, but for some reason it does matter what you put in that run()-method.

TLDR

Just putting the marker.setPosition(lat, lon)-call in the runnable wasn't enough. I had to put all of my marker handling code in the runnable (finding the markers, checking if they have a position, setting the new position, updating the map camera).

In detail

The problem

  • My app was receiving location updates from someone else via GCM.
  • With every location update, the coresponding marker had to be updated.
  • The first GCM was received by my application, but the next ones weren't.
  • No crashes, and the application was fully responsive, just no more GCM messages received after the first one.

What I discovered

  • My first thought was that the GCM server was just being slow.
  • But: While monitoring all console output I saw debug messages from GCM showing that the messages were arriving on the device, but not in the app!

What worked for me

  • Make sure all Google Maps code that needs to run when receiving the GCM is in a Runnable on the MainLooper.

Conslusion

  • It does make sense to put all GM code in a runnable, but seeing that there was no error output and the app was completely responsive it was very hard to discover that solution.

Use my code:

    private class UpdateThread extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            //...
            mMarker.setPosition(new LatLng(member.getLatitude(),member.getLongitude()));
        }
        @Override
        protected Void doInBackground(Void... params) {

            while(true)
            {
                try {
                    //...
                    publishProgress();

                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    UpdateThread updateThread = new UpdateThread();
    updateThread.execute();

Try this,

Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
         @Override
         public void run() {
              //your code
         }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!