java
java

Reputation: 1165

Pass 3D-array to Activity in Android

I am trying to pass a 3D-doublearray to another activity. I don know why I get NullpointerException? What is missing?

MainActivity

 Intent intent = new Intent(this, DataActivity.class);
 Bundle mBundle = new Bundle();
 mBundle.putSerializable("list", output_data);
 intent.putExtras(mBundle);
 startActivity(intent);

DataActivity (reveiver)

 Intent intent = new Intent();
 double[][][] params = (double[][][]) intent.getExtras().getSerializable("list");

And I know for certain that the 3d-array already is allocated in the MainActivity. I have tested that!

Would be glad if someone has any solution to this and could answer why I get a NullPointerException.

(Edit: NullpointerException at the line double[][][] params = ...)

Upvotes: 0

Views: 118

Answers (2)

Vedran Kopanja
Vedran Kopanja

Reputation: 1299

I'm pretty sure double[][][] doesn't implement Serializable. What you can do is make your own class like so:

public class MyArrayWrapper implements Serializable {

  private double[][][] arr;

  public MyArrayWrapper(double[][][] value) {
    arr = value;
  }

  public double[][][] getArray() {
    return arr;
  }
}

You then instantiate that wrapper class and put it as a serializable instead. Then when getting the serializable you do it like this:

MyArrayWrapper wrap = (MyArrayWrapper) intent.getExtras().getSerializable("list");
double[][]][] myArray = wrap.getArray();

Hope this helps!

Upvotes: 3

Neonamu
Neonamu

Reputation: 736

Maybe you need a class that implements serializable?

Passing data through intent using Serializable

In this example, they are using custom class and List<> to serialize in a bundle. You can create a class with the 3D array inside?

Upvotes: 1

Related Questions