Activity 1:
Bundle bundle = new Bundle();
bundle.putSerializable("CustomLevelData", LevelCreator.LCLevelData);
Intent i = new Intent(LevelCreatorPopout.this, GameView.class);
i.putExtras(bundle);
startActivity(i);
Activity 2:
LevelData=(int[][]) extras.getSerializable("CustomLevelData");
错误:
E/AndroidRuntime(16220):致命异常:main
E/AndroidRuntime(16220): java.lang.RuntimeException: 无法启动 Activity ComponentInfo{com.powerpoint45.maze/com.powerpoint45.maze.GameView}: java.lang.ClassCastException: java.lang.Object[] 无法转换到 int[][]
我已经搜索过,但没有找到关于 2d INT 数组传递的内容
最佳答案
如果你仍然想坚持使用 Serializable,你必须在第二个 Activity 中打破它并做一个循环。
Bundle dataBundle = getIntent().getExtras();
int[][] levelData = new int[3][3];
Object[] temp = (Object[])dataBundle.getSerializable("CustomLevelData");
if(temp != null){
levelData = new int[temp.length][];
for(int i = 0; i < temp.length; i++){
levelData[i] = (int[])temp[i];
}
}
关于java - Android 将 2d int 数组从一个 Activity 传递到另一个 ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19781553/