1

我正在尝试将 3 个字符串传递给一个新活动。通过调试,我发现我的变量在我将它们放入附加项时不为空,但当我尝试获取它们时它们为空

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

这是变量为空的被调用活动:

    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

4 回答 4

3

年、月等是整数而不是字符串,因此请使用

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

[编辑]

或者

String year = Integer.toString( extras.getInt( "year" ) );
于 2012-08-16T10:09:03.900 回答
0
String year=String.valueOf(extras.getInt("year"));
String month=String.valueOf(extras.getInt("month"));
String dayOfMonth=String.valueOf(extras.getInt("dayOfMonth"));
于 2012-08-16T10:10:48.770 回答
0

您正在传递整数值,

int year=extras.getInt("year");
        int month=extras.getInt("month");
        int dayOfMonth=extras.getInt("dayOfMonth");
于 2012-08-16T10:11:19.030 回答
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);

    }
于 2012-08-16T10:29:48.017 回答