2013-11-01 40 views
-1

我写了一些简单的游戏,就像一只猫一样。但是当我运行它时,它崩溃了。我真的不明白为什么。我的应用程序在启动时崩溃

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多

回答

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


     } 

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

“数组常量只能在初始化中使用”日食说 – Bringoff

+0

我编辑它,你需要添加'新ImageView的[] []' –

+0

哦,对不起, 我懂了。我怎么会忘记它?但是我学习java和android一天的时间) – Bringoff

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

您应该只保存ID,R.id.XXX 并在查询视图时使用findViewById。

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

0

你需要后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 

移动内部onCreate所有的初始化没有夸大,你不能使用findViewById初始化意见布局。如果没有,你会得到NullPointerException

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中)。