App force关闭时出现错误:java.lang.RuntimeException:无法启动活动组件信息
问题是,当我点击按钮时,应用程序强制关闭并给出一个错误。该应用程序是一个记忆游戏。当我点击按钮时,它将进入游戏页面App force关闭时出现错误:java.lang.RuntimeException:无法启动活动组件信息,java,android,grid-layout,Java,Android,Grid Layout,问题是,当我点击按钮时,应用程序强制关闭并给出一个错误。该应用程序是一个记忆游戏。当我点击按钮时,它将进入游戏页面 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_memory_game); GridLayout gridLayout = (GridLayo
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_memory_game);
GridLayout gridLayout = (GridLayout)
findViewById(R.id.grid_layout_4x4);
int numColumns = gridLayout.getColumnCount();
int numRows = gridLayout.getRowCount();
numberOfElements = numColumns +numRows;
buttons = new MemoryButtons[numberOfElements];
buttonGraphics = new int[numberOfElements/ 2];
buttonGraphics[0] = R.drawable.button_1;
buttonGraphics[1] = R.drawable.button_2;
buttonGraphics[2] = R.drawable.button_3;
buttonGraphics[3] = R.drawable.button_4;
buttonGraphics[4] = R.drawable.button_5;
buttonGraphics[5] = R.drawable.button_6;
buttonGraphics[7] = R.drawable.button_7;
buttonGraphics[8] = R.drawable.button_8;
buttonGraphicLocations = new int[numberOfElements];
shuffleButtonGraphics();
for (int r = 0; r < numRows; r++){
for (int c = 0; c < numColumns; c++){
MemoryButtons tempButton = new
MemoryButtons(this,r,c,buttonGraphics[buttonGraphicLocations[r*
numColumns * c]]);
tempButton.setId(View.generateViewId());
buttons[r * numColumns *c] = tempButton;
gridLayout.addView(tempButton);
}
}
}
protected void shuffleButtonGraphics(){
Random rand = new Random();
for(int i = 0; i < numberOfElements; i++){
buttonGraphicLocations[i] = i% (numberOfElements / 2);
}
for (int i = 0; i < numberOfElements; i++){
int temp = buttonGraphicLocations[i];
int swapIndex = rand.nextInt(16);
buttonGraphicLocations[i] = buttonGraphicLocations[swapIndex];
buttonGraphicLocations[swapIndex] = temp;
}
}
ERROR:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.game/com.example.game.MemoryGame}:
java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2485)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5601)
at java.lang.reflect.Method.invoke(Native Method) atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u memory\u game);
GridLayout=(GridLayout)
findViewById(R.id.grid\u布局\u 4x4);
int numColumns=gridLayout.getColumnCount();
int numRows=gridLayout.getRowCount();
numberOfElements=numColumns+numRows;
按钮=新存储器按钮[numberOfElements];
ButtongGraphics=新整数[numberOfElements/2];
按钮图形[0]=R.drawable.button_1;
按钮图形[1]=R.drawable.button_2;
按钮图形[2]=R.drawable.button_3;
按钮图形[3]=R.drawable.button_4;
按钮图形[4]=R.drawable.button_5;
按钮图形[5]=R.drawable.button_6;
按钮图形[7]=R.drawable.button_7;
按钮图形[8]=R.drawable.button_8;
buttonGraphicLocations=新整数[numberOfElements];
shuffleButtonGraphics();
对于(int r=0;r
原因:java.lang.ArrayIndexOutOfBoundsException:长度=4;index=4,位于com.example.game.MemoryGame.onCreate(MemoryGame.java:46)崩溃的原因是
rand.nextInt(16)代码>
生成0-16之间的随机数,但实际按钮大小为8
解决办法是
您应该使用numberOfElements=numColumns*numRows代码>而不是numberOfElements=numColumns+numRows代码>