Android java矩形数组不';行不通
我想做一个小迷宫游戏。为此,我现在有四堵墙,一个球可以击中。都是图像视图。我使用了Android java矩形数组不';行不通,java,android,arrays,rect,Java,Android,Arrays,Rect,我想做一个小迷宫游戏。为此,我现在有四堵墙,一个球可以击中。都是图像视图。我使用了Rect.intersect方法 为了从墙上得到矩形,我使用了ImageView.getHitRect。然后将矩形存储在矩形数组中。这就是一切似乎都失败的地方。当我使用方法getHitRect使用此数组中的矩形时,我得到以下错误: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Rect.
Rect.intersect
方法
为了从墙上得到矩形,我使用了ImageView.getHitRect
。然后将矩形存储在矩形数组中。这就是一切似乎都失败的地方。当我使用方法getHitRect
使用此数组中的矩形时,我得到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Rect.set(int, int, int, int)' on a null object reference
这是我的密码
private ImageView ball;
private ImageView wall[] = new ImageView[4];
private TextView text;
private SensorManager sManager;
private int a=300; //x position
private int b=300; //y position
int x=0;
int y=0;
Rect rect[] = new Rect[4];
Rect ballrect = new Rect();
float show = 1;
boolean allowedMovement[]={true,true,true,true};
int maxX=0;
int maxY=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ball = (ImageView) findViewById(R.id.ball);
text = (TextView) findViewById(R.id.info);
wall[0] = (ImageView) findViewById(R.id.wall1);
wall[1] = (ImageView) findViewById(R.id.wall2);
wall[2] = (ImageView) findViewById(R.id.wall3);
wall[3] = (ImageView) findViewById(R.id.wall4);
sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
//when this Activity starts
@Override
protected void onResume()
{
super.onResume();
/*register the sensor listener to listen to the gyroscope sensor, use the
callbacks defined in this class, and gather the sensor information as quick
as possible*/
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
}
//When this Activity isn't visible anymore
@Override
protected void onStop()
{
//unregister the sensor listener
sManager.unregisterListener(this);
super.onStop();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing.
}
public void move(int x, int y) {
RelativeLayout.LayoutParams alp = (RelativeLayout.LayoutParams) ball.getLayoutParams();
int maxMovementX=Math.abs(x);
int maxMovenentY=Math.abs(y);
int stepsTakenX=0;
int stepsTakenY=0;
while(maxMovementX>stepsTakenX || maxMovenentY>stepsTakenY) {
hitCheck();
//up 0, down 1, right 2, left 3
if (stepsTakenX < maxMovementX) {
stepsTakenX=stepsTakenX+1;
if (x > 0 && allowedMovement[2] == true) {//right
a = a - 1;
}
if (x < 0 && allowedMovement[3] == true) {//left
a = a + 1;
}
}
if (stepsTakenY < maxMovenentY) {
stepsTakenY=stepsTakenY+1;
if (y > 0 && allowedMovement[1] == true) {//down
b = b - 1;
}
if (y < 0 && allowedMovement[0] == true) {//up
b = b + 1;
}
}
alp.leftMargin = a;
alp.topMargin = b;
ball.setLayoutParams(alp);
}
}
public void hitCheck(){
//up 0, down 1, right 2, left 3
for(int i=0;i<4;i++){
allowedMovement[i]=true;
}
ball.getHitRect(ballrect);
for(int i=0;i<4;i++) {
wall[i].getHitRect(rect[i]);
if (Rect.intersects(rect[i], ballrect)) {
wall[i].setAlpha(show);
float wy = (ballrect.width() + rect[i].width()) * (ballrect.centerY() - rect[i].centerY());
float hx = (ballrect.height() + rect[i].height()) * (ballrect.centerX() - rect[i].centerX());
if (wy > hx) {
if (wy > -hx) {//top
allowedMovement[1] = false;
} else {//left
allowedMovement[2] = false;
}
} else {
if (wy > -hx) {//right
allowedMovement[3] = false;
} else {//bottom
allowedMovement[0] = false;
}
}
}
}
}
private-ImageView-ball;
私有ImageView墙[]=新ImageView[4];
私有文本查看文本;
私人经理;
私人INTA=300//x位置
私人int b=300//y位置
int x=0;
int y=0;
Rect Rect[]=新Rect[4];
Rect-ballrect=新的Rect();
浮动显示=1;
布尔allowedMovement[]={true,true,true,true};
int max=0;
int-maxY=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ball=(ImageView)findViewById(R.id.ball);
text=(TextView)findViewById(R.id.info);
墙[0]=(ImageView)findViewById(R.id.wall1);
墙[1]=(ImageView)findViewById(R.id.wall2);
墙[2]=(ImageView)findViewById(R.id.wall3);
墙[3]=(ImageView)findViewById(R.id.wall4);
sManager=(传感器管理器)getSystemService(传感器服务);
}
//此活动何时开始
@凌驾
受保护的void onResume()
{
super.onResume();
/*注册传感器侦听器以侦听陀螺仪传感器,使用
此类中定义的回调,并尽快收集传感器信息
尽可能*/
sManager.registerListener(这是sManager.getDefaultSensor(Sensor.TYPE_-ORIENTATION)、SensorManager.Sensor_-DELAY_-faster);
}
//当此活动不再可见时
@凌驾
受保护的void onStop()
{
//取消注册传感器侦听器
sManager.unregisterListener(此);
super.onStop();
}
@凌驾
已更改准确性上的公共无效信息(传感器arg0、内部arg1)
{
//什么也不做。
}
公共无效移动(整数x,整数y){
RelativeLayout.LayoutParams alp=(RelativeLayout.LayoutParams)ball.getLayoutParams();
int maxMovementX=Math.abs(x);
int maxmoventy=Math.abs(y);
int-stepsTakenX=0;
int-stepsTakenY=0;
而(maxMovementX>stepsTakenX | | MaxMoveEnty>stepsTakenY){
hitCheck();
//向上0,向下1,右2,左3
if(stepsTakenX0&&allowedMovement[2]==true){//right
a=a-1;
}
如果(x<0&&allowedMovement[3]==true){//left
a=a+1;
}
}
if(stepsTakenY0&&allowedMovement[1]==true){//down
b=b-1;
}
如果(y<0&&allowedMovement[0]==true){//up
b=b+1;
}
}
alp.leftMargin=a;
alp.topMargin=b;
ball.setLayoutParams(alp);
}
}
公共无效hitCheck(){
//向上0,向下1,右2,左3
对于(inti=0;i-hx){//top
allowedMovement[1]=假;
}否则{//左
allowedMovement[2]=假;
}
}否则{
如果(wy>-hx){//正确
allowedMovement[3]=假;
}否则{//bottom
allowedMovement[0]=false;
}
}
}
}
}
它对ballrect没有问题,但对数组中的实体有问题
Rect Rect[]=new Rect[4]代码>仅分配对象引用的存储位置,不分配实际对象
尝试插入rect[i]=new rect()代码>在wall[i]之前.getHitRect(rect[i])代码>