Henrik
Henrik

Reputation: 1157

Accessing non-static method from activity class?

Im currently trying to develop a little game where i want to use a class that extends SurfaceView and have it to draw in i thread (similar to LunarLander). However when i would like to change contentview to the one from xml, the one im not drawing in i was trying to call a method from my surfaceview class that is in activity class that will change contentview by setContentView, i get an RuntimeException:

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

It's probably just because im somehow new to android and java development but i don't understand why it will work when the method is a static method but not otherwise?

(method in my Start class that extends activity)

    public void simulationDone()
{
  .....
}

(trying to access it)

new Start().simulationDone();

Upvotes: 0

Views: 1290

Answers (1)

Matthew
Matthew

Reputation: 44919

A couple of problems:

First, you shouldn't call setContentView multiple times.

Second, your surface view will need a reference to your activity. I tend to define listeners on my views that need to communicate back with an activity. In a custom SurfaceView called MySurfaceView:

public static interface Listener {
    public void simulationDone();
}

private Listener listener;

public void setListener(Listener listener) {
    this.listener = listener;
}

Then have your activity implement MySurfaceView.Listener and call mySurfaceView.setListener(this) when you create your surface view.

Upvotes: 2

Related Questions