gpt4 book ai didi

java - 在包内传递字节数组将变为空

转载 作者:行者123 更新时间:2023-12-01 16:58:37 28 4
gpt4 key购买 nike

我试图将一个字节数组从 Activity A 传递到 Activity B,但由于某种原因该变量突然变为空。我一直在试图弄清楚发生了什么事,但我还没有找到解决方案,也许你可以帮助我。这是我的 Activity A 的代码:

    @Override
public void respond( String desc_plato, byte[] imag_plato ){
System.out.println("des plato: "+desc_plato);
System.out.println("image plato: "+imag_plato);

if( imag_plato != null )
{
System.out.println("HERE IMAG_PLATO IS NOT NULL");
}

Intent intentDetalle = new Intent( this, PlatosDetalle.class );
intentDetalle.putExtra("desc_plato", desc_plato);
intentDetalle.putExtra("imag_plato ", imag_plato);
startActivity( intentDetalle );
}
}

Activity B 的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
// excluding common code here... then:
System.out.println("PlatosDetalle onCreate");

Bundle PlatosIntent = getIntent().getExtras();
String desc_plato = PlatosIntent.getString("desc_plato");
byte[] imagen_plato = PlatosIntent.getByteArray("imag_plato");
// I GOT THIS CODE FROM ANOTHER ANSWER, TO CHECK KEYS ON INTENT
/**************************************************/
for (String key : PlatosIntent.keySet()) {
Object value = PlatosIntent.get(key);
System.out.println("BUNDLE KEYS:"+ String.format("%s %s\n", key,
value.toString()));
}
System.out.println( imagen_plato );
/************************************************/


if( imagen_plato == null )
{
System.out.println("HERE IMAGEN_PLATO IS NULL");
finish();
}
}

在 Activity B 的代码中,即使在 for 循环中打印键->值之后,变量“imagen_plato”也已经为空...

这是我的 logCat,或者至少是我使用 System.out.println 执行的日志:

04-16 02:04:35.915: I/System.out(5072): des plato: Pizza especial para clientes vegetarianos
04-16 02:04:35.925: I/System.out(5072): image plato: [B@41a80170
04-16 02:04:35.925: I/System.out(5072): HERE IMAG_PLATO IS NOT NULL
04-16 02:04:35.955: I/Choreographer(5072): Skipped 132 frames! The application may be doing too much work on its main thread.
04-16 02:04:36.015: I/System.out(5072): PlatosDetalle onCreate
04-16 02:04:36.015: I/System.out(5072): BUNDLE KEYS:desc_plato Pizza especial para clientes vegetarianos
04-16 02:04:36.015: I/System.out(5072): BUNDLE KEYS:imag_plato [B@41757c48
04-16 02:04:36.015: I/System.out(5072): null
04-16 02:04:36.015: I/System.out(5072): HERE IMAGEN_PLATO IS NULL

感谢任何帮助,谢谢!

最佳答案

这里:

 intentDetalle.putExtra("imag_plato ", imag_plato);
^

使用imag_plato键名传递空格。使用不带空格字符的键:

intentDetalle.putExtra("imag_plato", imag_plato);

关于java - 在包内传递字节数组将变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666529/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com