Ondraw() Method Work Only When I Creating My Object In The Ondraw()
I got a problem. I don't know why I can't create a object for example in my iniGame() method. When I create a object from the GameObject class and use the method render() in the on
Solution 1:
You are getting a NullPointerException
because your initGame
is probably not called. At least not on the Object where onDraw
is called.
What I mean is that there are two instances of your GameView
. The first instance is the one you create manually using:
gameView = new GameView(this);
The second instance is created from the XML-File, since you are defining:
<de.fhflensburg.manichja.barrier51.GameView
android:id="@+id/gameView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
So this second instance is created automatically by the App. And you don't know at the beginning, which of the three constructors is called. There are two things you should change:
- In your activity, replace
gameView = new GameView(this);
withgameView = (GameView) findViewById(R.id.gameView);
. This will give you the correct instance of your GameView. Please refer to the View-Doc for more details. - In your
GameView
class, add a call toinitGame
in all three constructors. This ensures that the elements are initialized, no matter what constructor is called.
Post a Comment for "Ondraw() Method Work Only When I Creating My Object In The Ondraw()"