我试图在android上创建简单日历,当我尝试将参数(日期,月份,年份)从MainActivity发送到另一个Acvitity(参数为0)时遇到问题

MainActivity.java

public void onClick(View view) { calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView calendarView, int i, int i1, int i2) { Bundle localBundle = new Bundle(); localBundle.putInt("Date", i2); localBundle.putInt("Month", i1); localBundle.putInt("Year", i); } }); if(view == buttonGetDate1) { Bundle localBundle = new Bundle(); Intent localIntent = new Intent(MainActivity.this, TestActivity.class); localIntent.putExtras(localBundle); startActivity(localIntent); finish(); } }

TestAcvitity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    datedisplay1 = (TextView) findViewById(R.id.date_display1);
    datedisplay1.setText("Date: ");

    buttonBack1 = (Button) findViewById(R.id.buttonBack);
    buttonBack1.setOnClickListener(this);
    Bundle intent = getIntent().getExtras();
    int day = intent.getInt("date");
    int month = intent.getInt("month");
    int year = intent.getInt("year");
    Log.d(TAG, "THIRD LOG : "  + day  + " / " + month + " / " + year);
    datedisplay1.setText("Date: " + day  + " / " + month + " / " + year);
}


But it's show 0/0/0

我该如何解决?
ps。对不起,我英语水平不好。

编辑:我尝试调试日志后
EDIT2:更新了TestActivity

in this pic, intent have value are Date = 30 Year = 2017 Month = 4 but it's show null on application

最佳答案

数据为空,因为您再次单击重新初始化了包,我对您的代码做了一些更改,请尝试一下

{
 //inside your activity on create scope
 int dateData=0;
 int monthData=0;
 int yearData=0;
 calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(CalendarView calendarView, int i, int i1, int i2) {
            dateData= i2;
            monthData= i1;
            yearData= i;


//Log here whether u getting date here?
        }
    });


public void onClick(View view) {

    if(view == buttonGetDate1) {

       Bundle localBundle = new Bundle();
       localBundle.putInt("Date", dateData);
       localBundle.putInt("Month", monthData);
       localBundle.putInt("Year", yearData);


        Intent localIntent = new Intent(MainActivity.this, TestActivity.class);
        localIntent.putExtras(localBundle);
        startActivity(localIntent);
        finish();

    }
  }
}//Activity scope ends


TestAcvitity.java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

datedisplay1 = (TextView) findViewById(R.id.date_display1);
datedisplay1.setText("Date: ");

buttonBack1 = (Button) findViewById(R.id.buttonBack);
buttonBack1.setOnClickListener(this);
Bundle intent = getIntent().getExtras();
int day = intent.getInt("Date");
int month = intent.getInt("Month");
int year = intent.getInt("Year");
Log.d(TAG, "THIRD LOG : "  + day  + " / " + month + " / " + year);
datedisplay1.setText("Date: " + day  + " / " + month + " / " + year);
}


希望这会帮助你。

10-06 13:18