Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Intent.putExtra发送数组_Java_Android_Android Intent_Android Activity_Bundle - Fatal编程技术网

Java 使用Intent.putExtra发送数组

Java 使用Intent.putExtra发送数组,java,android,android-intent,android-activity,bundle,Java,Android,Android Intent,Android Activity,Bundle,我在活动A中有一个整数数组: int array[] = {1,2,3}; 我想将该变量发送到活动B,因此我创建了一个新的intent并使用putExtra方法: Intent i = new Intent(A.this, B.class); i.putExtra("numbers", array); startActivity(i); 在活动B中,我获得以下信息: Bundle extras = getIntent().getExtras(); int arrayB = extras.ge

我在活动A中有一个整数数组:

int array[] = {1,2,3};
我想将该变量发送到活动B,因此我创建了一个新的intent并使用putExtra方法:

Intent i = new Intent(A.this, B.class);
i.putExtra("numbers", array);
startActivity(i);
在活动B中,我获得以下信息:

Bundle extras = getIntent().getExtras();
int arrayB = extras.getInt("numbers");

但这并不是真正发送数组,我只是在arrayB上得到值“0”。我一直在寻找一些示例,但没有找到任何示例。

您正在使用数组设置额外的示例。然后尝试获取单个int

您的代码应该是:

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

此代码发送整数值数组

初始化数组列表

List<Integer> test = new ArrayList<Integer>();
test.add(1);
test.add(2);
test.add(3);
Intent intent=new Intent(this, targetActivty.class);
将数组列表值发送到目标活动

intent.putIntegerArrayListExtra("test", (ArrayList<Integer>) test);
startActivity(intent);
intent.putIntegerArrayListExtra(“测试”,(ArrayList)测试);
星触觉(意向);
这里您可以获得targetActivity的值

Intent intent=getIntent();
ArrayList<String> test = intent.getStringArrayListExtra("test");
Intent-Intent=getIntent();
ArrayList测试=intent.getStringArrayListExtra(“测试”);

哎哟!我专注于putExtra和getExtras语法,我没有意识到错误是如此明显:D谢谢@Kitinz+1表示对社区非常友好。。。我喜欢:)我想你会在看到这段代码后知道你在哪里犯了错误……)我需要的答案是在你的问题中。这是如何使用
.getExtras()
我所需要的。
Intent intent=getIntent();
ArrayList<String> test = intent.getStringArrayListExtra("test");