我的应用程序在启动时崩溃了

时间:2013-11-01 08:54:39

标签: java android crash

我写了一些像猫一样的简单游戏。但是当我跑它时它崩溃了。我真的不明白为什么。

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>

崩溃日志:

11-01 09:44:23.810:D / dalvikvm(18847):延迟启用CheckJNI 11-01 09: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):致命异常:主要 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):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137) 11-01 09:44:23.910:E / AndroidRuntime(18847):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 11-01 09:44:23.910:E / AndroidRuntime(18847):在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 11-01 09:44:23.910:E / AndroidRuntime(18847):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256) 11-01 09:44:23.910:E / AndroidRuntime(18847):在android.os.Handler.dispatchMessage(Handler.java:99) 11-01 09:44:23.910:E / AndroidRuntime(18847):在android.os.Looper.loop(Looper.java:137) 11-01 09:44:23.910:E / AndroidRuntime(18847):在android.app.ActivityThread.main(ActivityThread.java:5103) 11-01 09:44:23.910:E / AndroidRuntime(18847):at java.lang.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):在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):在android.app.Instrumentation.newActivity(Instrumentation.java:1061) 11-01 09:44:23.910:E / AndroidRuntime(18847):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) 11-01 09:44:23.910:E / AndroidRuntime(18847):... 11更多

5 个答案:

答案 0 :(得分:0)

SO不是调试器,但因为你的错误非常愚蠢:

您正在尝试在setContentView!

之前使用findViewById

此代码在onCreate:

之前执行
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)}};

您所要做的就是将代码更改为

private ImageView[][] gameField;

并在setContentView:

之后添加
gameField = new ImageView[][]{{(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)}};

答案 1 :(得分:0)

你不能这样做

  私人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)}};

你应该只保存id,R.id.XXX 当查询视图时使用findViewById。

如果视图尚不存在,则无法通过id查询视图。

答案 2 :(得分:0)

您需要在onCreate

之后在setContentView内移动所有初始化
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // must be first
    iv =(ImageView)findViewById(R.id.enemy1_1);  // then initialization
    //  same for the rest of the initializations

如果不展开布局,则无法使用findViewById初始化视图。如果没有,你会得到NullPointerException

答案 3 :(得分:0)

问题出在这里。

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)}};

您无法将变量初始化为字段..您必须将它们放在onCreate()方法中。方法findViewById()只能在onCreate之后使用...更具体地说,在调用setContentView()之后(在onCreate中)。

答案 4 :(得分:0)

在设置内容视图之前,无法初始化组件。

您可以将其设置为:

    public class MainActivity extends Activity {

        private static final long duration = 50000;
        private static final long interval = 1500;

     // ID of Image View
public static int[] CellID=new int[]{
         R.id.mycell01,R.id.mycell02,R.id.mycell03,R.id.mycell04,
         R.id.mycell05,R.id.mycell06,R.id.mycell07,R.id.mycell08,R.id.mycell09};
     // Image View name
static ImageView img01,img02,img03,img04,img05,img06,img07,img08,img09;
    // Array of ImageView
public static ImageView[] ImageViewImg=new ImageView[]{
        img01,img02,img03,img04,img05,img06,img07,img08,img09};


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    for (int i = 0; i < ImageViewImg.length; i++) {
            ImageViewImg[i]=(ImageView)findViewById(CellID[i]);


        }

        }