将日期参数发送给另一个 Activity 但值为零 // android studio

Sent date parameter to another Activity but value is zero // android studio

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

的 Acvitity 时遇到问题

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

希望对您有所帮助。