开发者

Android Child Classes and Separate Java Files

I am making an application for android which could grow somewhat large over time. What I would like to do is implement into the main class sub files for logic. For example:

The main activity uses a surfaceview.

class Battle extends SurfaceV开发者_运维百科iew implements SurfaceHolder.Callback {

I want to add all logic related to touch events in a file called touchActions.java

public class touchActions extends Battle {

It appears to work fine with no errors in Eclipse. But when I try to run it on my phone, I get a null pointer exception for the following line.

if (_touch.checkHitBox(1)) {

_touch being initiated after the Battle class is declared.

public touchActions _touch;

What is the proper way to do this, or what could be causing the nullpointerexception?

EDIT:

The goal here is organization of code, so that I don't end up with one gigantic file of code. Can I make an inner class while still using a different file? If you know of a tutorial, that would be great too.


_touch being initiated after the Battle class is declared.

public touchActions _touch;

not initiated ... just declared u need to initiatie it with something like this

_touch = new touchActions();

EDIT:

if public touchActions _touch; is declared in Battle class you do it all wrong ...

it should be something like this

class Battle extends SurfaceView implements SurfaceHolder.Callback {
   public touchActions _touch;
   public Battle (){
     _touch = new touchActions(this);
     _touch.callingMethodFromTouch();
   }
}

//should not extends Battle
public class touchActions {
  Battle parent_;
  public touchActions (Battle parent){
     parent_ = parent;
  }

  .... rest of implemetation you can call parent_.methodFromBattleClass(); here
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜