0

I'm creating an android application with 2 layouts. In one layout the user input data and I would like to keep this data and use it on a different layout.

I try using 2d array, but it seems that array values are not sent to the second layout.

On my first layout

grid = new double[2][long];
for( int i=0; i<long; i++ )
{
    grid[0][i]=Data[i];
    grid[1][i]=Value2;  
}

public double[][] sendGrid() 
{
    return grid;
}

When I want to call the 2d grid array on the second Layout I have...

try{
    Layout1 mapInstance = new Layout1y();
    double[][] dataX = mapInstance.sendGrid();
    Log.i("dataXLength",""+dataX.length);
}
catch(Exception e)
{
    Log.i("-OK",e.toString());
}

The result is: 04-13 10:31:45.357: I/-OK(28588): java.lang.NullPointerException

Any idea on how can I send the 2d array to my second layout?

Thank you

blackpanther
  • 10,998
  • 11
  • 48
  • 78
Gerardo Abdo
  • 1,150
  • 3
  • 10
  • 16

2 Answers2

1

there are couple of methods to do that but i think in your case its best suited if you make your 2d array static.

static double grid = new double[2][long];

Now in next activity access like this

double[][] dataX =Activity1.grid[][];   
Abubakar
  • 1,022
  • 10
  • 20
  • This option seems to be very simple. Thank you very much, it is working now! – Gerardo Abdo Apr 13 '13 at 16:13
  • 1
    @GerardoAbdo You should use intents to pass data between activities. Using static variables is not recommended. http://www.youtube.com/watch?v=_CruQY55HOk. The guy in the video gives a big warning using static variables. http://developer.android.com/training/articles/perf-tips.html#PreferStatic. – Raghunandan Apr 13 '13 at 18:00
0

By "Layout", I assume you mean "Activity". You have a number of options to pass data between activities, all of which are explained nicely on this question: How do I pass data between Activities in Android application?

Community
  • 1
  • 1
IncrediApp
  • 10,303
  • 2
  • 33
  • 24