Skip to content Skip to sidebar Skip to footer

.getextras()' On A Null Object Reference

I am trying to pass a few values between my activities to make a game work. However, the new activity after passing the information always returns null when I try to .getExtras().

Solution 1:

Move

Bundleextras= getIntent().getExtras();

ArrayList<Integer> players = extras.getIntegerArrayList("players");
intcurrentPlayer= extras.getInt("newPlayerNum");

into onCreate(). getIntent() needs a Context which isn't available before onCreate()

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_three_sign_game);

    // hereBundleextras= getIntent().getExtras();

     ArrayList<Integer> players = extras.getIntegerArrayList("players");
     intcurrentPlayer= extras.getInt("newPlayerNum");
}

Solution 2:

Wait until onCreate to get the intent. Your trying to getIntent() when the class is instantiated instead of when the Activity is active.

Then just assign the values found to a field and other methods inside that class can access the field.

Post a Comment for ".getextras()' On A Null Object Reference"