Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android将2d int数组从一个活动传递到另一个错误_Java_Android_Arrays_2d - Fatal编程技术网

Java Android将2d int数组从一个活动传递到另一个错误

Java Android将2d int数组从一个活动传递到另一个错误,java,android,arrays,2d,Java,Android,Arrays,2d,活动1: Bundle bundle = new Bundle(); bundle.putSerializable("CustomLevelData", LevelCreator.LCLevelData); Intent i = new Intent(LevelCreatorPopout.this, GameView.class); i.putExtras(bundle); startActivity(i); 活动2: LevelData=(int[][]) extras.getSerial

活动1:

Bundle bundle = new Bundle();
bundle.putSerializable("CustomLevelData",  LevelCreator.LCLevelData);
Intent i = new Intent(LevelCreatorPopout.this, GameView.class);
i.putExtras(bundle);
startActivity(i);
活动2:

LevelData=(int[][]) extras.getSerializable("CustomLevelData");
错误:
E/AndroidRuntime(16220):致命异常:main
E/AndroidRuntime(16220):java.lang.RuntimeException:无法启动活动组件信息{com.powerpoint45.maze/com.powerpoint45.maze.GameView}:java.lang.ClassCastException:java.lang.Object[]无法转换为int[][]


我已经搜索了2d INT数组,但没有找到传递的任何内容。与其使用Serializable,不如从性能角度使用它来传递非原始数据

不确定这是否是最好的主意,但您可以定义一个包含2d数组并实现Parcelable的类。然后,您可以使用以下命令从活动传递该类的实例:

Intent intent = this.getIntent();
// Assume MyClass is the class which contains the 2d-array
intent.putExtra("key", myclassObj); //value being the instance/object of MyClass that you want to pass
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
MyClass mc = (MyClass)bundle.getParcelable("key"); 
您可以使用以下命令在另一个活动中检索它:

Intent intent = this.getIntent();
// Assume MyClass is the class which contains the 2d-array
intent.putExtra("key", myclassObj); //value being the instance/object of MyClass that you want to pass
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
MyClass mc = (MyClass)bundle.getParcelable("key"); 

如果您仍然想继续使用Serializable,那么必须在第二个活动中中断它并执行循环

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];
    }
}
Bundle-dataBundle=getIntent().getExtras();
int[][]levelData=新int[3][3];
Object[]temp=(Object[])dataBundle.getSerializable(“CustomLevelData”);
如果(温度!=null){
levelData=新整数[温度长度][];
对于(int i=0;i
我做了什么:

上课

package com.powerpoint45.maze;
导入android.os.packet; 导入android.os.Parcelable

公共类SerializableCustomData实现Parcelable{

public int[][] ints;

public int[][] getints() {
    return ints;
}

public void setints(int[][] ints) {
    this.ints = ints;
}

public SerializableCustomData() {
    ints = new int[1][1];
}

public SerializableCustomData(Parcel in) {
    ints = (int[][]) in.readSerializable();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeSerializable(ints);

}
public static final Parcelable.Creator<SerializableCustomData> CREATOR = new Parcelable.Creator<SerializableCustomData>() {

    @Override
    public SerializableCustomData createFromParcel(Parcel in) {
        return new SerializableCustomData(in);
    }

    @Override
    public SerializableCustomData[] newArray(int size) {
        return new SerializableCustomData[size];
    }
};
抓住它

SerializableCustomData myParcelable = extras.getParcelable("parcel");
            LevelData = myParcelable.getints();