Passing an array with android-intent

2019-06-14 15:25发布

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条回答
仙女界的扛把子
2楼-- · 2019-06-14 16:10

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");
查看更多
登录 后发表回答