传递一个阵列,Android的意图 [英] Passing an array with android-intent
本文介绍了传递一个阵列,Android的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的achartengine库,我想通过一个意图通过图点的阵列,这样,当我点击一个按钮,它开辟了从主活动的数据图。目前我使用的是硬codeD阵列称为X在活动A,并试图将它传递给b活动。
活动答:
公共无效lineGraphHandler(查看视图){
线图线=新线图();
意向lineIntent = line.getIntent(本);
lineIntent.putExtra(点,X);
startActivity(lineIntent);
}
活动B:
公共类线图{INT [] X = getIntent(空).getIntArrayExtra(点);公共意向getIntent(上下文的背景下){
// INT []×= {1,2,3,4,5,6,7,8,9,10}; // x值! INT [] Y = {30,34,45,57,77,89,100,111,123,145}; // y的值!
不过我正在运行空指针错误和logcat的说:
由Java.lang.reflect.Method.InvocationTargetException造成了
解决方案
你应该使用:
A级:
int数组[] = {1,2,3}; 意图I =新意图(A.this,B.class);
i.putExtra(数字,数组);
startActivity(ⅰ);
B类:
捆绑额外= getIntent()getExtras()。
INT [] = arrayB extras.getIntArray(数字);
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"
解决方案
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");
这篇关于传递一个阵列,Android的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文