gpt4 book ai didi

java - 我想只编写一次这个方法并多次调用它来为各种 TextView 分配值

转载 作者:太空宇宙 更新时间:2023-11-04 11:02:34 25 4
gpt4 key购买 nike

private void getClubTiming(TextView txtClubTime) {

TextView txtClub = txtClubTime;
calendar = Calendar.getInstance();
CalendarHour = calendar.get(Calendar.HOUR_OF_DAY);
CalendarMinute = calendar.get(Calendar.MINUTE);
timepickerdialog = new TimePickerDialog(this,
(view, hourOfDay, minute) -> {
if (hourOfDay == 0) {
hourOfDay += 12;
format = "am";
} else if (hourOfDay == 12) {
format = "pm";
} else if (hourOfDay > 12) {
hourOfDay -= 12;
format = "pm";
} else {
format = "am";
}
Log.i("@sandy","ClubTiming: "+txtClub);
if (txtClub!=null){
txtClub.setText(String.format("%02d:%02d" + format, hourOfDay, minute));


}


}, CalendarHour, CalendarMinute, false);
timepickerdialog.show();
}

因为我只想始终调用此方法 OnClickListener 来分配 TimePicker 值,但给我空指针异常,异常是 txtClub.setText(String.format("%02d:%02d" + format, hourOfDay, minute));我在设置 TextView 的值时遇到问题。请帮助我......谢谢。

最佳答案

Initialization all data in the initView method

在您的代码中尝试一下。

public class SignUpSecondActivity extends AppCompatActivity {
private LayoutInflater layoutInflater;
private EditText txtFrom, txtTo;
private Calendar calendar;
private TimePickerDialog timepickerdialog;
private int CalendarHour, CalendarMinute;
private String format;
private TextView txtClubTime,txtFromSunday,txtToSunday,txtFromMonday,txtToMonday;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
initView();
getTxtFromSunday().setOnClickListener(view -> getClubTiming(txtClubTime));
}

private void initView() {
txtClubTime = (TextView) findViewById(R.id.txtClubTime);
txtFromSunday = (TextView) findViewById(R.id.txtFromSunday);
txtToSunday = (TextView) findViewById(R.id.txtToSunday);
txtFromMonday = (TextView) findViewById(R.id.txtFromMonday);
txtToMonday = (TextView) findViewById(R.id.txtToMonday);
}

private void getClubTiming(TextView txtClubTime) {

TextView txtClub = txtClubTime;
calendar = Calendar.getInstance();
CalendarHour = calendar.get(Calendar.HOUR_OF_DAY);
CalendarMinute = calendar.get(Calendar.MINUTE);
timepickerdialog = new TimePickerDialog(this,
(view, hourOfDay, minute) -> {
if (hourOfDay == 0) {
hourOfDay += 12;
format = "am";
} else if (hourOfDay == 12) {
format = "pm";
} else if (hourOfDay > 12) {
hourOfDay -= 12;
format = "pm";
} else {
format = "am";
}
Log.i("@sandy","ClubTiming: "+txtClub);
if (txtClub!=null){
txtClub.setText(String.format("%02d:%02d" + format, hourOfDay, minute));
Log.i("@sandy","ClubTiming- "+txtClub);
}

Log.i("@sandy", "Time: " + String.format("%02d:%02d" + format, hourOfDay, minute));
}, CalendarHour, CalendarMinute, false);
timepickerdialog.show();
}

}

关于java - 我想只编写一次这个方法并多次调用它来为各种 TextView 分配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46741989/

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