我写了一些简单的游戏,就像一只猫一样。但是当我运行它时,它崩溃了。我真的不明白为什么。我的应用程序在启动时崩溃
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final long duration = 50000;
private static final long interval = 1500;
private ImageView[][] gameField = {{(ImageView)findViewById(R.id.enemy1_1),(ImageView)findViewById(R.id.enemy1_2),(ImageView)findViewById(R.id.enemy1_3)},
{(ImageView)findViewById(R.id.enemy2_1),(ImageView)findViewById(R.id.enemy2_2),(ImageView)findViewById(R.id.enemy2_3)},
{(ImageView)findViewById(R.id.enemy3_1),(ImageView)findViewById(R.id.enemy3_2),(ImageView)findViewById(R.id.enemy3_3)}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GameCountDownTimer gameCountDownTimer = new GameCountDownTimer(duration, interval);
gameCountDownTimer.start();
}
public class GameCountDownTimer extends CountDownTimer {
Game game = new Game();
ImageView imgLose = (ImageView)findViewById(R.drawable.cat_sing); //Lost cat
ImageView imgTouch = (ImageView)findViewById(R.drawable.cat_pirate); //Touched cat
public GameCountDownTimer(long duration, long interval) {
super(duration, interval);
}
@Override
public void onFinish() {
for (int i = 0; i < Game.rows; i++) {
for (int j = 0; j < Game.cols; j++) {
gameField[i][j].setImageResource(0);
}
}
Toast resultToast = Toast.makeText(getApplicationContext(), "You missed " + game.gameScore() + " cats:)",Toast.LENGTH_LONG);
resultToast.setGravity(Gravity.CENTER, 0, 0);
resultToast.show();
}
@Override
public void onTick(long millisUntilFinished) {
boolean[][] newEnemies = game.generateEnemy();
int score = 0;
for (int i = 0; i < Game.rows; i++) {
for (int j = 0; j < Game.cols; j++) {
if (gameField[i][j].getResources() == imgLose.getResources()) {
score++; //Если кот пропущен, увеличиваем счетчик
}
if (gameField[i][j].getResources() == imgTouch.getResources()) {
gameField[i][j].setImageResource(0); //Убираемся за отшлепанными котами
}
if (newEnemies[i][j]) {
gameField[i][j].setImageResource(R.drawable.cat_sing); //Выпускаем новых котов
}
}
}
game.setScore(score);
}
}
}
Game.java
import java.util.Random;
/**
* User: Bringoff
* Date: 30.10.13
* Time: 8:07
*/
public class Game {
static final int cols = 3;
static final int rows = 3;
private int score;
private boolean[][] arEnemies = null;
public int results = 0;
public void setScore(int score) {
this.score+= score;
}
public int gameScore() {
return score;
}
public boolean[][] generateEnemy() {
arEnemies = new boolean[rows][cols];
Random random = new Random();
for (int i = 0; i < rows; i++) {
for (int j = 0; i < cols; j++) {
arEnemies[i][j] = random.nextBoolean();
}
}
return arEnemies;
}
}
activity_main.xml中
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:id="@+id/main_l">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/results_view"
android:id="@+id/tvResults"
android:layout_weight="0.75"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textStyle="bold"
android:textSize="28dp"
android:typeface="normal"
android:gravity="center_vertical|center_horizontal" />
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/enemy1_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitCenter"
android:onClick="enemies_onClick"></ImageView>
<ImageView
android:id="@+id/enemy1_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitCenter"
android:onClick="enemies_onClick"></ImageView>
<ImageView
android:id="@+id/enemy1_3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitCenter"
android:onClick="enemies_onClick"></ImageView>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/enemy2_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitCenter"
android:onClick="enemies_onClick"></ImageView>
<ImageView
android:id="@+id/enemy2_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitCenter"
android:onClick="enemies_onClick"></ImageView>
<ImageView
android:id="@+id/enemy2_3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitCenter"
android:onClick="enemies_onClick"></ImageView>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/enemy3_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitCenter"
android:onClick="enemies_onClick"></ImageView>
<ImageView
android:id="@+id/enemy3_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitCenter"
android:onClick="enemies_onClick"></ImageView>
<ImageView
android:id="@+id/enemy3_3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitCenter"
android:onClick="enemies_onClick"></ImageView>
</TableRow>
</TableLayout>
日志崩溃的:
9月11日至一日:44:23.810:d/dalvikvm(18847):晚使能CheckJNI 9月11日至一日:44:23.910:d/AndroidRuntime(18847):关闭VM 11-01 09:44:23.910:W/dalvikvm(18847):threadid = 1:线程退出时未捕获的异常(group = 0x416f6700) 11-01 09:44:23.910:E/AndroidRuntime(18847) :致命例外:main 11-01 09:44:23.910:E/AndroidRuntime(18847):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.bringoff.touchthemall/com.bringoff.touchthemall.MainActivity}:java .lang.NullPointerException 11-01 09:44:23.910:E/AndroidRuntime(18847):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137) 11-01 09:44:23.910:E/AndroidRuntime(18847):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 11-01在android.app中使用E/AndroidRuntime(18847):at android.app.ActivityThread.access $ 600(ActivityThread.java:141) 11-01 09:44:23.910:E/AndroidRuntime(18847) .ActivityThread $ H.handleMessage(ActivityThread.java:1256) 11-01 09:44:23.910:E/AndroidRuntime(18847):at android.os.Handler.dispatchMessage(Handler.java:99) 11-01 09 :44:23.910:E/AndroidRuntime(18847):at android.os.Looper.loop(Looper.java:137) 11-01 09:44:23.910:E/AndroidRuntime(18847):at android.app.ActivityThread .main(ActivityThread.java:5103) 11-01 09:44:23.910:E/AndroidRuntime(18847):at java.lan g.reflect.Method.invokeNative(Native Method) 11-01 09:44:23.910:E/AndroidRuntime(18847):at java.lang.reflect.Method.invoke(Method.java:525) 11-01 09 :44:23.910:E/AndroidRuntime(18847):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 11-01 09:44:23.910:E/AndroidRuntime(18847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-01 09:44:23.910:E/AndroidRuntime(18847):at dalvik.system.NativeStart.main(Native Method) 11 -01 09:44:23.910:E/AndroidRuntime(18847):导致:java.lang.NullPointerException 11-01 09:44:23.910:E/AndroidRuntime(18847):at android.app.Activity.findViewById(Activity .java:1853) 11-01 09:44:23.910:E/AndroidRuntime(18847):at com.bringoff.touchthemall.MainActivity。(MainActivity.java:17) 11-01 09:44:23.910:E/AndroidRuntime(18847):at java.lang.Class.newInstanceImpl(Native Method) 11-01 09:44:23.910:E/AndroidRuntime(18847):at java.lang .Class.newInstance(Class.java:1130) 11-01 09:44:23.910:E/AndroidRuntime(18847):at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 11-01 09:44 :23.910:E/AndroidRuntime(18847):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) 11-01 09:44:23.910:E/AndroidRuntime(18847):...11多
“数组常量只能在初始化中使用”日食说 – Bringoff
我编辑它,你需要添加'新ImageView的[] []' –
哦,对不起, 我懂了。我怎么会忘记它?但是我学习java和android一天的时间) – Bringoff