Search code examples
androidgraphicsrunnablelooper

Android handler & Looper.prepare() issue


Im creating a space shooter game for android, im currently implementing the shooting methods but getting some problems i cant figure out when trying to create new shots and drawing them.

i have this method:

    public void createShot(int time){

     if(shotCreated==true && time<=0){
         System.out.println("Funkar");
         Shot temp = new Shot(shipX,700);
         System.out.println("Funkar2");
         shotList.add(temp);

     }else if(time>=10){
         tick=0;
     }
}

that is called from the run() method But i get the error: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Iv'e tried researching it here on the forum but can quite figure out how to solve it, can someone help me out?

regards, Fred


Solution

  • in you run() method place these two line at the top

    Looper.myLooper();
    Looper.prepare();