0

Okay, so

  • new Zombie variable created.
    private Zombie zombieA;
  • zombie variable initialized

    zombieA = new Zombie(1);
  • calls constructor in Zombie class:

    public Zombie(int type) {
    this.type = type;
    x=200;
    y=100;
    dx=1;
    paintA.setColor(Color.RED);}
    

Basically, I want the game class to create a new Zombie, with the type as 1 which will go through a switch and case to determine which type of zombie to create (level 1=10). The problem is when I run it my application force closes and i get the an error on:

zombieA = new Zombie(1);

from the initial class and an error on:

public Zombie(int type) {

from the Zombie class. I've been going over it again and again and i just can't see the problem, anyone notice anything wrong?

Ali
  • 187
  • 1
  • 2
  • 9
  • stack trace? I was just reading the LogCat, it says java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() – Ali Apr 14 '12 at 15:54
  • 1
    @AlexBath - Full stack trace please: line numbers, classes and methods called. You can't see the problem, because there is no problem with the constructor parameters.. – Ishtar Apr 14 '12 at 16:23

2 Answers2

1

It's hard to tell without the stack trace, but I suspect that the problem is in this line:

paintA.setColor(Color.RED);

Are you sure that paintA has been properly initialized? it seems to me that it's null and causing a NullPointerException in your constructor. If that's the case, be sure to instantiate it first, something like this:

paintA = new ...
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    ugh, it's always the little things -_-. thanks, that worked perfectly. I had it set up like Paint paintA,paintB = new Paint();and just had to change it to initialize in the constructor, thanks for the help! – Ali Apr 14 '12 at 16:46
0

Are you calling the Zombie class from within a worker thread? You'll need to do this from the UI thread, there are some examples here: Can't create handler inside thread that has not called Looper.prepare()

Community
  • 1
  • 1
  • I believe I'm calling it from within the UI thread,. My grasp of threads is weak at best but I have a game class with a class inside it GamePanel that extends surfaceview and implements runnable, and I'm calling the Zombie class from within the run method, thanks I'll try changing it up and get back to you. – Ali Apr 14 '12 at 16:25
  • If you're getting "`Can't create handler inside thread that has not called Looper.prepare()`" you're definitely not calling it on the UI-thread. – Jens Apr 14 '12 at 16:27