我用Android工作室制作游戏。到目前为止一切都还可以,但是今天运行我的应用程序后,它在启动活动后崩溃了。我长时间搜索错误但我找不到任何东西。
MatheGame.java:
import android.graphics.Point;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MatheGame extends AppCompatActivity {
private TextView scoreLabel;
private TextView startLabel;
private ImageView box;
ImageView orange;
// Size
private int frameHeight;
private int boxSize;
private int screenWidth;
private int screenHeight;
// Position
private int boxY;
private int orangeX;
private int orangeY;
// Speed
private int boxSpeed;
private int orangeSpeed;
// Score
private int score = 0;
// Initialize Class
private Handler handler = new Handler();
private Timer timer = new Timer();
// Status Check
private boolean action_flg = false;
private boolean start_flg = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scoreLabel = (TextView) findViewById(R.id.scoreLabel);
startLabel = (TextView) findViewById(R.id.startLabel);
box = (ImageView) findViewById(R.id.box);
orange = (ImageView)findViewById(R.id.orange);
// Get screen size.
WindowManager wm = getWindowManager();
Display disp = wm.getDefaultDisplay();
Point size = new Point();
disp.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
boxSpeed = Math.round(screenHeight / 60);
orangeSpeed = Math.round(screenWidth / 60);
orange.setX(-80);
orange.setY(-80);
scoreLabel.setText("Score : 0");
}
public void changePos() {
hitCheck();
// Orange
orangeX -= orangeSpeed;
if (orangeX < 0) {
orangeX = screenWidth + 20;
orangeY = (int) Math.floor(Math.random() * (frameHeight - orange.getHeight()));
}
orange.setX(orangeX);
orange.setY(orangeY);
// Move Box
if (action_flg == true) {
// Touching
boxY -= boxSpeed;
} else {
// Releasing
boxY += boxSpeed;
}
// Check box position.
if (boxY < 0) boxY = 0;
if (boxY > frameHeight - boxSize) boxY = frameHeight - boxSize;
box.setY(boxY);
scoreLabel.setText("Score : " + score);
}
public void hitCheck() {
// Orange
int orangeCenterX = orangeX + orange.getWidth() / 2;
int orangeCenterY = orangeY + orange.getHeight() / 2;
if (0 <= orangeCenterX && orangeCenterX <= boxSize &&
boxY <= orangeCenterY && orangeCenterY <= boxY + boxSize) {
score += 10;
orangeX = -10;
}
}
public boolean onTouchEvent(MotionEvent me) {
if (start_flg == false) {
start_flg = true;
FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
frameHeight = frame.getHeight();
boxY = (int)box.getY();
boxSize = box.getHeight();
startLabel.setVisibility(View.GONE);
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
changePos();
}
});
}
}, 0, 20);
} else {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
action_flg = true;
} else if (me.getAction() == MotionEvent.ACTION_UP) {
action_flg = false;
}
}
return true;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BACK:
return true;
}
}
return super.dispatchKeyEvent(event);
}
}
activity_mathe_game.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.eitisoft.mathetrainer.MatheGame">
<TextView
android:id="@+id/scoreLabel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Score : 300"
android:textSize="18sp"
android:paddingLeft="10dp"
android:gravity="center_vertical"/>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/startLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap to Start"
android:textSize="30sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="130dp"/>
<ImageView
android:id="@+id/box"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/box"
android:layout_gravity="center_vertical"/>
<ImageView
android:id="@+id/orange"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/punkt"/>
</FrameLayout>
</RelativeLayout>
错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setX(float)' on a null object reference
at com.eitisoft.mathetrainer.MatheGame.onCreate(MatheGame.java:85)
答案 0 :(得分:1)
您正在设置错误的布局文件。因此ImageView为null,如果您要在null引用上使用任何属性,那么它将崩溃。更新你的onCreate
<ol class="listAddress">
<li>365 W Northwest Hwy, Palatine, IL 60067, USA</li>
<li>365 W Northwest Hwy, Palatine, IL 60067, USA</li>
<li>365 W Northwest Hwy, Palatine, IL 60067, USA</li>
<li>365 W Northwest Hwy, Palatine, IL 60067, USA</li>
<li>365 W Northwest Hwy, Palatine, IL 60067, USA</li>
</ol>
而不是
setContentView(R.layout.activity_mathe_game);