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

4
  • use shared preferences or pass the grid value through the intent. Commented Apr 13, 2013 at 15:46
  • Please post compilable Java code. Commented Apr 13, 2013 at 15:49
  • I will test intent and let you know, thank you Commented Apr 13, 2013 at 15:50
  • stackoverflow.com/questions/4780835/… Commented Apr 13, 2013 at 15:52

2 Answers 2

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[][];   
Sign up to request clarification or add additional context in comments.

2 Comments

This option seems to be very simple. Thank you very much, it is working now!
@GerardoAbdo You should use intents to pass data between activities. Using static variables is not recommended. youtube.com/watch?v=_CruQY55HOk. The guy in the video gives a big warning using static variables. developer.android.com/training/articles/….
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.