目前,我无法将从“活动”中的CalendarView中选择的日期传递给片段以在TextView中显示所选日期。所以这是我在Activity中的代码,其中CalendarView是
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_planner);
cvSelectPlannerDate = (CalendarView) findViewById(R.id.cvSelectPlannerDate);
fragmentPlannerDayView = new FragmentPlannerDayView();
// get current date
Calendar date = Calendar.getInstance();
// for date format
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
// set string to format current date
String curDate = sdf.format(date.getTime());
// print date in log cat
Log.d("CUR_DATE", curDate);
// get date changed
cvSelectPlannerDate.setOnDateChangeListener(new OnDateChangeListener() {
public void onSelectedDayChange(CalendarView view, int year,
int month, int day) {
// add one because month starts at 0
month = month + 1;
// output to logcat
String newDate = year + "-" + month + "-" + day;
Log.d("NEW_DATE", newDate);
send(day, month, year);
}
});
}
public void send(int day, int month, int year) {
Bundle date = new Bundle();
date.putInt("Day", day);
date.putInt("Month", month);
date.putInt("Year", year);
FragmentPlannerDayView sendToFragment = new FragmentPlannerDayView();
sendToFragment.setArguments(date);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTrans = fragmentManager.beginTransaction();
fragmentTrans.replace(R.id.content_frame, sendToFragment);
fragmentTrans.addToBackStack(FragmentPlannerDayView.class.getName());
fragmentTrans.commit();
}
我设法在logcat中显示当前日期和新日期 - 我从另一个答案得到了这一部分:Android CalendarView: How do I get the date in correct format?
下一部分代码在我的Fragment类中,
public class FragmentPlannerDayView extends Fragment {
Context context;
public FragmentPlannerDayView() {
}
Calendar calendar;
TextView tvPlannerDate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planner_day_view,
container, false);
context = rootView.getContext();
tvPlannerDate = (TextView) rootView.findViewById (R.id.tvPlannerDate);
Bundle bundle = this.getArguments();
int day= bundle.getInt("Day");
int month = bundle.getInt("Month");
int year = bundle.getInt("Year");
String date = day + "-" + month + "-" + year;
tvPlannerDate.setText(date);
return rootView;
}
}
我尝试做的是从上一个活动中的捆绑中检索我的日期。但是,当我尝试运行这些代码时,所有发生的事情都是我的应用程序崩溃(不幸的是,您的应用程序已停止工作)。我无法找出应用程序中的问题。我想要的只是让用户在CalendarView中选择一个日期,一旦选择,它们将被重定向到相关的片段,从而所选日期将显示在TextView中。
顺便说一下,这里是logcat消息