我要从一个Activity传几个byte[]值,因为我传的值数量不定,所以我采取的循环的方式传值,跟踪显示每个buffer值都是不一样的,但是到另外一个Activity获取到的buffer1,buffer2...,,值都是最后一个获取到的buffer,比如我传了4个buffer,就是所有都是buffer4的值,请教一下代码哪里的问题,下面是代码。
传值Activity:
String bufferNum="";
while(isAlive)
{
in.read(buffer, 0, 24);
count++;
bufferNum="buffer"+count;
bundle.putString(bufferNum, buffer.toString());
if(in.available()==0)
{
isAlive=false;
bundle.putInt("count", count);
}
}
取值Activity:
String bufferNum="";
byte buffer[] = new byte[24];
Bundle bundle=getIntent().getExtras();
int count=bundle.getInt("count");
for(int num=1;num<=count;num++)
{
bufferNum="buffer"+num;
buffer=bundle.getByteArray(bufferNum);
}
传值Activity:
String bufferNum="";
while(isAlive)
{
in.read(buffer, 0, 24);
count++;
bufferNum="buffer"+count;
bundle.putString(bufferNum, buffer.toString());
if(in.available()==0)
{
isAlive=false;
bundle.putInt("count", count);
}
}
取值Activity:
String bufferNum="";
byte buffer[] = new byte[24];
Bundle bundle=getIntent().getExtras();
int count=bundle.getInt("count");
for(int num=1;num<=count;num++)
{
bufferNum="buffer"+num;
buffer=bundle.getByteArray(bufferNum);
}
解决方案 »
- 怎样调用Android手机右上角状态栏的闹钟图标
- Android的Tab选项卡中Activity跳转的问题
- 请教android遍地都是的sp问题
- 做android测试暴出的错误。坐等求解
- 严重的问题...关于java.awt
- 竞OPhone SDN版主 享超豪华礼遇 OPhone SDN 版主火热招募中!!!
- 求Android的书!
- 初学安卓,编写的程序无法运行,可否看下是什么问题
- surfaceDestroyed方法点击home键后,执行的比较慢,如何解决
- 新手Fragment Activity问题
- 如何在ListView中嵌套TableLayout
- 准备学习android开发 ,求推荐一些好的学习资料或博客
bundle.putByteArray(headBufferNum, headBuffer);
bundle.putByteArray(bufferNum, buffer);这么传的
in.read(buffer, 0, 24);这句是给buffer赋值,跟踪了,每次都不同
bundle.putByteArray(bufferNum, buffer);这句往bundle里面写入,应该没覆盖吧,bufferNum是变的
代码应该改成:in.read(buf, 0, buf.length);
count++;
bufferNum = "buffer" + count;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
sb.append(buf[i]);
}
bundle.putString(bufferNum, sb.toString());
if (in.available() == 0) {
isAlive = false;
bundle.putInt("count", count);
}
String num="";
String str="";
Bundle bundle=this.getIntent().getExtras();
int count=bundle.getInt("count");
for(int i=1;i<=count;i++){
num="buffer"+i;
str=bundle.getString(num);
Log.d("tag",num+" : "+str);
}
谢谢指导,我后来补充了,我是传的byte[],不是String,那个代码贴错了。
bundle.putByteArray(bufferNum, buffer);
buffer=bundle.getByteArray(bufferNum);
String bufferNum="";
while(isAlive)
{
byte buffer[] = new byte[24];
in.read(buffer, 0, 24);
count++;
bufferNum="buffer"+count;
bundle.putString(bufferNum, buffer.toString());
if(in.available()==0)
{
isAlive=false;
bundle.putInt("count", count);
}
}