Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7162195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:41:41+00:00 2026-05-28T13:41:41+00:00

I am having a problem updating the view in android every x seconds. To

  • 0

I am having a problem updating the view in android every x seconds.

To get to know how android works I am writing a small game.
It has a GameController which holds the game loop. Inside the loop, the logic is executed and afterwards the gui will be informed about the changes.

The problem is that the changes cannot be seen in the view. The view stays the same until the game loop finishes and updates then only once.

Here is my code (the important parts):

GameController.java:

IGui gui;

public void play() {
    while (playing) {
        getAndProcessInput();
        updateGui();
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
        }
    }
}

private void updateGui() {
    gui.setPlayer(x, y);
    // ...
}

GameActivity.java:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.GridView1);
    TextAdapter = new TextAdapter(this);
    gridview.setAdapter(textAdapter);

    GameController c = new GameController();

    // here, the game loop shoud start.
    // what i tried:

    // new Thread(c).start();   <-- causes crash

    // c.play();     <-- causes view to frease until game loop is done

    this.runOnUiThread(c);  <-- causes view to frease until game loop is done
}

TextAdapter.java:

public class TextAdapter extends BaseAdapter implements IGui {

    private final Context context;
    private final String[] texts;

    public TextAdapter(Context context) {
        this.context = context;
        texts = new String[height * width];
        for (int i = 0; i < texts.length; i++) {
            texts[i] = " ";
        }
    }

    public int getCount() {
        return height * width;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv;
        if (convertView == null) {
            tv = new TextView(context);
            tv.setLayoutParams(new GridView.LayoutParams(25, 25));
        } else {
            tv = (TextView) convertView;
        }

        tv.setText(texts[position]);
        return tv; // <-- this happens only when game loop is done, not everytime something changed
    }

    @Override 
    public void setPlayer(int x, int y) {
        texts[width * y + x] = "X";
        // this.notifyDataSetChanged();  <-- this does not help (view still freases) 
        //                                   gridview.invalidateViews(); does not help either
    }
}

I googled a lot and tried a lot as well (and I do know that similar questions where asked here already, but they did not help me either), but somehow it just does not work.
I cannot get it do work that the view and logic run on different theads in android, and if they run on the same thread the logic blocks the view.
Any help would be greatly appreciated.

// edit:

If I try new Thread(c).start(); LogCat sais:
java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare()

And if I add Looper.prepare(); :
java.lang.RuntimeException: Unable to start activity ComponentInfo{GameActivity}: java.lang.RuntimeException: Only one Looper may be created per thread

If I try this.runOnUiThread(c); there are no errors.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:41:42+00:00Added an answer on May 28, 2026 at 1:41 pm

    First, this doesn’t seems like the way to crate a game, you will need to use SurfaceView, or GLSurfaceView to better do what you want.
    You can also look for Cocos2d for android, it’s a 2D platform (that was ported from iPhone) that makes you life easier:
    http://code.google.com/p/cocos2d-android/
    I muse warn you though, I tried it a couple months back, and it was not production grade yet, it did crash from time to time.

    Anyway, if you still want to continue heading your way I’ll try answering your question:
    I think you are messing too much with the way these stuff should work. try understanding first how handlers work with threads.

    Do anything you want on your thread:

       new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        try
                        {
                            calculateGameChangesHere();
                            handler.sendEmptyMessage(SUCCESS);
                        }
                        catch (Exception e)
                        {
                            handler.sendEmptyMessage(FAILURE);
                        }
                    }
                }).start();
    

    When your data is ready, tell the handler to put it in a view and show it:

    protected Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                if (msg.what == SUCCESS)
                {
                    setCalculatedDataToaView(); // the data you calculated from your thread can now be shown in one of your views.
                }
                else if (msg.what == FAILURE)
                {
                    errorHandlerHere();//could be your toasts or any other error handling...
                }
            }
        };
    

    This goes to everything that requires heavy processing/networking that shouldn’t block your UI thread.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problem with Rails plugin attachment_fu . On every upload, I get validation
I am having a problem with the textbox text updating in my view. I
Am updating a 2D game I made to have an isometric view, problem is
I am having problem updating table cell value using jQuery 1.4.2. it all works
I'm having a problem updating a row after an inline edit. My ColModel is:
I'm having a problem updating a table and im sure its pretty straight forward
Hi I'm having a problem updating child objects in the following scenario. The mappings
I'm having a problem updating a control on my ui from a thread created
I am having a problem updating several rows of a mysql table using a
I am having a problem with updating the PATH variable. What I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.