Android intent.extras returns null

2.4k Views Asked by At

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);
    }
4

There are 4 best solutions below

0
On BEST ANSWER

year, month etc are ints not Strings so use

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

[edit]

or

String year = Integer.toString( extras.getInt( "year" ) );
0
On
String year=String.valueOf(extras.getInt("year"));
String month=String.valueOf(extras.getInt("month"));
String dayOfMonth=String.valueOf(extras.getInt("dayOfMonth"));
0
On

You are passing Integer values,

int year=extras.getInt("year");
        int month=extras.getInt("month");
        int dayOfMonth=extras.getInt("dayOfMonth");
0
On

While you getting write this code

    Bundle extras=getIntent().getExtras();
     if(extras!=null)
    {
    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);

    }