Error when i call asyncService.getLeaderBoard(gameName, max, this); on andriod gameupdate

0 votes

Error when i call  asyncService.getLeaderBoard(gameName, max, this); on andriod gameupdate 

Error message 

Can't create handler inside thread that has not called Looper.prepare()

Please how can i call this method from main thread when my gamestate is game over ?

Is there a way to call mainthread from another thread?

asked Jun 25, 2015 in App42 Cloud API-BaaS by Oti Chibueze (10 points)

1 Answer

0 votes

Hi,

In Android if you are doing some operation in background Thread,and want to update respective Game UI once data received. In this case you have to move from background thread to UI Thread.

In your case you are updating UI in background Thread so I would like to suggest you to use following code.

activity.runOnUiThread(new Runnable() {
  public void run() {
    //You can write Ui code here
  }
});

Let me know if it helps.

Thanks

Vishnu Garg

answered Jun 25, 2015 by Vishnu Garg (674 points)
I have an activity class and view class  here is what the view class looks like where I call update and draw So where do I place this code is it on the general run ()  where the update draw method is or I place the method call inside

        thread = new TimePuzzleThread(holder);
        thread.start();
       
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        try{
            editor = settings.edit();
            SaveGameData();
            editor.commit();
        }
        catch(Exception ex){}      
    }                                          

private SurfaceHolder mSurfaceHolder;
    public class TimePuzzleThread extends Thread{
        public TimePuzzleThread(SurfaceHolder surfaceHolder){
            mSurfaceHolder = surfaceHolder;
        }
       
        @Override
        public void run(){
            while (true){
                //breaks the thread here when
                Canvas c = null;
                try{
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized(mSurfaceHolder){
                        OnUpdate();
                        doDraw(c);                               

                    }
                }
                catch(Exception ex){}
                finally
                    if (c != null){mSurfaceHolder.unlockCanvasAndPost(c);}
                }
            }   
        }
You can place the in onCreate method of Activity once you got the result you can draw it on a View by using runOnUiThread function that I have shared earlier.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...