Android intent.extras返回null

时间:2012-08-16 10:06:12

标签: android android-intent android-activity extras

我正在尝试将3个字符串传递给新活动。通过调试我发现当我把它们放在额外​​的时候我的变量不是null但是当我尝试获取它们时它们是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);                          
            }
        });

这是变量为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 个答案:

答案 0 :(得分:3)

年,月等不是字符串,所以使用

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

[edit]

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

答案 1 :(得分:0)

String year=String.valueOf(extras.getInt("year"));
String month=String.valueOf(extras.getInt("month"));
String dayOfMonth=String.valueOf(extras.getInt("dayOfMonth"));

答案 2 :(得分:0)

您传递的是整数值,

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

答案 3 :(得分:0)

  

在您编写此代码时

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

    }