Passing an array with android-intent

1.7k views Asked by At

I'm using the achartengine library and I would like to pass an array of graph point through an intent so that when I click on a button it opens up a graph with the data from the main activity. At the moment I am using a hard coded array called x in Activity A and trying to pass it to activity B.

Activity A:

public void lineGraphHandler (View view) {
    LineGraph line = new LineGraph();
    Intent lineIntent = line.getIntent(this);
    lineIntent.putExtra("points", x);
    startActivity(lineIntent);
}

Activity B:

public class LineGraph{

int[] x = getIntent(null).getIntArrayExtra("points");

public Intent getIntent(Context context) {
    //int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // x values!

    int[] y =  { 30, 34, 45, 57, 77, 89, 100, 111 ,123 ,145 }; // y values!

However I am getting runtime null pointer errors and logcat says:

"Caused by Java.lang.reflect.Method.InvocationTargetException"
1

There are 1 answers

0
Miki Franko On BEST ANSWER

you should use:

class A:

  int array[] = {1,2,3};

  Intent i = new Intent(A.this, B.class);
  i.putExtra("numbers", array);
  startActivity(i);

class B:

Bundle extras = getIntent().getExtras();
int[] arrayB = extras.getIntArray("numbers");