gpt4 book ai didi

java - 发送日期参数到另一个Activity但值为零//android studio

转载 作者:行者123 更新时间:2023-12-02 13:17:29 26 4
gpt4 key购买 nike

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

MainActivity.java

public void onClick(查看 View ) {
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@覆盖
公共(public)无效onSelectedDayChange(CalendarView calendarView,int i,int i1,int i2){
bundle localBundle = new Bundle();
localBundle.putInt("日期", i2);
localBundle.putInt("月份", i1);
localBundle.putInt("年份", i);
}
});
如果( View ==按钮GetDate1){
bundle localBundle = new Bundle();
Intent localIntent = new Intent(MainActivity.this, TestActivity.class);
localIntent.putExtras(localBundle);
startActivity(localIntent);
结束();
}
}

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

我该如何修复它?附:抱歉我的英语水平不好。

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

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

最佳答案

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

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

希望这对您有帮助。

关于java - 发送日期参数到另一个Activity但值为零//android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43710878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com