Passing an array with android-intent

Deadly 提交于 2019-12-08 14:03:33

问题


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:


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");


来源:https://stackoverflow.com/questions/27215000/passing-an-array-with-android-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!