I'm new at Android programming and I'm coding a game but I'm having a problem when trying to add an object to an ArrayList.
I have the class Obstacle:
public class Obstacle extends AndroidApplication {
private int posX;
private int posY;
public Obstacle(int posX, int posY) {
setPosX(posX);
setPosY(posY);
}
public void setPosX(int objectPosX) {...}
public int getPosX() {...}
public void setPosY(int objectPosY) {...}
public int getPosY() {...}
}
When trying to do this in another class:
obstacles.add(new Obstacle(randomBetween(50, 150), 100));
I get this error:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
I did some research and I found that probably I'm trying to access Android UI toolkit from outside the UI thread. I don't know how is it possible since apparently I didn't do anything unusual, but...
I learned that I can call the function runOnUiThread
in order to access the UI thread, but I dont know exactly how to do it in my code. Is it something like this?
new Thread()
{
public void run()
{
myactivity.this.runOnUiThread(new runnable()
{
public void run()
{
obstacles.add(new Obstacle(randomBetween(50, 150), 100));
}
});
}
}.start();
I would appreciate any help.