简体   繁体   English

在bundle中传递字节数组将变为null

[英]Passing a byte array inside bundle becomes null

I'm trying to pass a byte array from activity A to activity B but for some reason the variable suddenly becomes null. 我正在尝试将字节数组从活动A传递到活动B,但由于某种原因,变量突然变为空。 I've been trying to figure out what's going on but I haven't found a solution, maybe you can help me. 我一直在试图找出正在发生的事情,但是我还没有找到解决方案,也许您可​​以帮助我。 Here is my code for activity A: 这是我的活动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 );
    }
}

Code on activity B: 活动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();
    }
}

In code for activity B, even right after printing the keys->values in the for loop the variable "imagen_plato" is already null... 在活动B的代码中,即使在打印for循环中的keys-> values之后,变量“ imagen_plato”也已经为空...

Here is my logCat, or at least the logs I do with System.out.println: 这是我的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

Any help is appreciated, thank you!!! 任何帮助表示赞赏,谢谢!!!

Here: 这里:

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

Passing space with imag_plato key name. 使用imag_plato键名的传递空间。 use key with-out space character : 使用不带空格的键:

intentDetalle.putExtra("imag_plato", imag_plato);

As @ρяσѕρєя-k pointed out, use proper key to put and get the byte array. 正如@ρяσsρєя-k所指出的那样,使用适当的键放置并获取字节数组。 Better to create some constants and use them in your code as key, so that you don't have to type your keys as literals every time. 最好创建一些常量,然后在代码中将它们用作键,这样就不必每次都将键键入为文字。 This will help in removing all these kind of problems in future. 这将有助于将来消除所有此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM