Search code examples
androidandroid-bundle

unable to send values from one activity to other in Android


I've created a bundle and put values onto it and jumped to the next activity,for some reason its not able to get the values at the other end,it says null object reference,but i have my values with me,

public void rsa_key(String s){
        Intent intent = new Intent(getBaseContext(), SomeClass.class);
        //Create the bundle
        Bundle bundle = new Bundle();

//Add your data to bundle
        //String hello=null;
        bundle.putString("id", txId);
        bundle.putString("cardamt", cardAmount);
        bundle.putString("cardconv", cardConvFee);
//Add the bundle to the intent
        intent.putExtras(bundle);




        startActivity(intent);

    }

In the other activity

String txId = bundle.getString("id");
        String cardConvFee = bundle.getString("cardconv");
        String cardAmount = bundle.getString("cardamt");

Solution

  • try this:

    public void rsa_key(String s){
            Intent intent = new Intent(getBaseContext(), SomeClass.class);
    
            //String hello=null;
            intent.putExtras("id", txId);
            intent.putExtras("cardamt", cardAmount);
            intent.putExtras("cardconv", cardConvFee);
    
    
    
    
    
            startActivity(intent);
    
        }
    

    in your second activity onCreate method

    Bundle bundle = getIntent().getExtras();
    String txId = bundle.getString("id");
            String cardConvFee = bundle.getString("cardconv");
            String cardAmount = bundle.getString("cardamt");