Search code examples
androidandroid-intentandroid-activityextras

Android intent.extras returns null


I am trying to pass 3 strings to a new activity. By debugging I have discovered that my variables are not null when I put them in extras but when I try to get them they are null

calendarView.setOnDateChangeListener(new OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month,
                    int dayOfMonth) 
            {

                 //Toast.makeText(getApplicationContext(), ""+dayOfMonth, 0).show();

                Intent myIntent = new Intent(CalendarActivity.this, DateDayActivity.class);
                myIntent.putExtra("year", year);
                myIntent.putExtra("month", month);
                myIntent.putExtra("dayOfMonth", dayOfMonth);
                CalendarActivity.this.startActivity(myIntent);                          
            }
        });

this is the called activity where the variables are null:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_date_day);

    Bundle extras=getIntent().getExtras();

    String year=extras.getString("year");
    String month=extras.getString("month");
    String dayOfMonth=extras.getString("dayOfMonth");

    String date=dayOfMonth+"/"+month+"/"+year;

    TextView tv=(TextView)findViewById(R.id.textView1);
    tv.setText(date);
    }

Solution

  • year, month etc are ints not Strings so use

    int year = extras.getInt( "year" );
    

    [edit]

    or

    String year = Integer.toString( extras.getInt( "year" ) );