Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中从另一个类调用方法?_Java_Android - Fatal编程技术网

Java 如何在android中从另一个类调用方法?

Java 如何在android中从另一个类调用方法?,java,android,Java,Android,我在android studio上了两门课,学习一个游戏。板条箱级和玩家级。我想知道:我如何在板条箱类中调用player类?我试图通过Player thePlayer=newplayer(getContext())来实现这一点;但这只是不断给我错误的获取上下文部分 public class Crate { public static int acrossCrate; public int upDownCrate; Player thePlayer = new Player(getContext(

我在android studio上了两门课,学习一个游戏。板条箱级和玩家级。我想知道:我如何在板条箱类中调用player类?我试图通过Player thePlayer=newplayer(getContext())来实现这一点;但这只是不断给我错误的获取上下文部分

public class Crate {
public static int acrossCrate;
public int upDownCrate;
Player thePlayer = new Player(getContext());  //<--GIVES ERROR

public Crate(Context context) {

    int rect = 1000;
    int height = context.getResources().getDisplayMetrics().heightPixels;
    int width = context.getResources().getDisplayMetrics().widthPixels;

    int startPosX = (width/2)-(rect/2);
    int startPosY = (height/2)-(rect/2);

    acrossCrate = startPosX +300;
    upDownCrate = startPosY +300;

}


public class Player {
public static int across;
public int upDown;
int boardWidth;
int boardHeight;
int startPosX;
int startPosY;
int stopPosX;
int stopPosY;

public Player(Context context) {

    int rect = 1000;
    boardHeight = context.getResources().getDisplayMetrics().heightPixels;
    boardWidth = context.getResources().getDisplayMetrics().widthPixels;

    startPosX = (boardWidth/2)-(rect/2);
    startPosY = (boardHeight/2)-(rect/2);
    stopPosX = (boardWidth/2)+(rect/2);
    stopPosY = (boardHeight/2)+(rect/2);

    across = startPosX+500;
    upDown = startPosY+500;
}
公共级板条箱{
公共静态利率;
公共集装箱;

Player thePlayer=new Player(getContext());//将代码的第一部分转换为以下内容:

public class Crate {
public static int acrossCrate;
public int upDownCrate;
Player thePlayer;

public Crate(Context context) {

    int rect = 1000;
    int height = context.getResources().getDisplayMetrics().heightPixels;
    int width = context.getResources().getDisplayMetrics().widthPixels;

    int startPosX = (width/2)-(rect/2);
    int startPosY = (height/2)-(rect/2);

    acrossCrate = startPosX +300;
    upDownCrate = startPosY +300;

    //define your player here
   thePlayer = new Player(context);

}

Player-thePlayer=new-Player(getContext());
更改为
Player-thePlayer;
并使用
thePlayer=new-Player(context);
crater
类构造中你应该真正阅读一本关于Java基础知识的书。
public class Crate {
public static int acrossCrate;
public int upDownCrate;
Player thePlayer ;  

public Crate(Context context) {
thePlayer = new Player(context);
int rect = 1000;
int height = context.getResources().getDisplayMetrics().heightPixels;
int width = context.getResources().getDisplayMetrics().widthPixels;

int startPosX = (width/2)-(rect/2);
int startPosY = (height/2)-(rect/2);

acrossCrate = startPosX +300;
upDownCrate = startPosY +300;

}


 public class Player {
public static int across;
public int upDown;
  int boardWidth;
  int boardHeight;
 int startPosX;
 int startPosY;
 int stopPosX;
  int stopPosY;

  public Player(Context context) {

     int rect = 1000;
      boardHeight =   context.getResources().getDisplayMetrics().heightPixels;
boardWidth = context.getResources().getDisplayMetrics().widthPixels;

startPosX = (boardWidth/2)-(rect/2);
startPosY = (boardHeight/2)-(rect/2);
stopPosX = (boardWidth/2)+(rect/2);
stopPosY = (boardHeight/2)+(rect/2);

across = startPosX+500;
upDown = startPosY+500;
}