Android app crash with putExtra

I'm trying to do a little app for myself and I can't do a putExtra on an intent, it crashes when I try to do it. After some search I can't find what I'm doing wrong, I'm pasting code who works perfectly and still crash

Main Activity Code to put extras :

Intent i = new Intent(getApplicationContext(), ShinyCounter.class);

i.putExtra("pokemon", poke);
i.putExtra("method", meth);
i.putExtra("methodAbrev", methAbrev);

All variables comes from EditText like this :

String poke = pokeName.getText().toString();

And the get Extras on my ShinyCounter Activity :

Bundle extras = getIntent().getExtras();

String pokeName = extras.getString("pokemon");
String method = extras.getString("method");
String methodAbrev = extras.getString("methodAbrev");

The error is : java.lang.RuntimeException: Unable to instantiate activity ComponentInfo: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference

1 answer

  • answered 2017-06-17 19:43 Aalishan Ansari

    You can do something like below in main activity

     Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
          i.putExtra("Value1", "Android ExplicitIntent");  
          // Set the request code to any code you like, you can identify the  
          // callback via this code  

    You can do something like below in ActivityTwo

     Bundle extras = getIntent().getExtras();  
    String value1 = extras.getString("Value1");