Hello.
I have this error when i want to implement a Fragment with a WeekView : "java.lang.IllegalStateException: You must provide a MonthChangeListener".
I really don't understand because I have a MonthChangeListener !
I looked for the reason of this error on internet but I didn't find...
It works when I made an activity with WeekView but I don't manage to make a fragment with it, always this error.
I paste my code, if you know why this error occured, please let me know !
public class Cal extends Fragment implements MonthLoader.MonthChangeListener,WeekView.EventClickListener, WeekView.EventLongPressListener,
WeekView.EmptyViewLongPressListener{
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public Intent impor;
public Intent settings;
public Intent compte;
View v;
LayoutInflater inflater;
private int index;
private static WeekView mWeekView;
private static WeekViewEvent e;
private static ArrayList<WeekViewEvent> mNewEvents;
public Cal() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Cal.
*/
// TODO: Rename and change types and number of parameters
public static Cal newInstance(String param1, String param2) {
Cal fragment = new Cal();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
inflater=getActivity().getLayoutInflater();
v=inflater.inflate(R.layout.weekview, null);
// Get a reference for the week view in the layout.
mWeekView = (WeekView) v.findViewById(R.id.weekView);
// The week view has infinite scrolling horizontally. We have to provide the events of a
// month every time the month changes on the week view.
mWeekView.setMonthChangeListener(this);
// Show a toast message about the touched event.
mWeekView.setOnEventClickListener(this);
// Set up empty view click listener.
mWeekView.setEmptyViewLongPressListener(this);
mWeekView.setEventLongPressListener(this);
// Set up a date time interpreter to interpret how the date and time will be formatted in
// the week view. This is optional.
setupDateTimeInterpreter(false);
// Initially, there will be no events on the week view because the user has not tapped on
// it yet.
mNewEvents = new ArrayList<WeekViewEvent>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.weekview, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
// Populate the week view with the events that was added by tapping on empty view.
List<WeekViewEvent> events = new ArrayList<WeekViewEvent>();
ArrayList<WeekViewEvent> newEvents = getNewEvents(newYear, newMonth);
events.addAll(newEvents);
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 3);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.MONTH, newMonth - 1);
startTime.set(Calendar.YEAR, newYear);
Calendar endTime = (Calendar) startTime.clone();
endTime.add(Calendar.HOUR, 1);
endTime.set(Calendar.MONTH, newMonth - 1);
WeekViewEvent event = new WeekViewEvent(1, getEventTitle(startTime), startTime, endTime);
event.setColor(getResources().getColor(R.color.red));
events.add(event);
startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 3);
startTime.set(Calendar.MINUTE, 30);
startTime.set(Calendar.MONTH, newMonth-1);
startTime.set(Calendar.YEAR, newYear);
endTime = (Calendar) startTime.clone();
endTime.set(Calendar.HOUR_OF_DAY, 4);
endTime.set(Calendar.MINUTE, 30);
endTime.set(Calendar.MONTH, newMonth-1);
event = new WeekViewEvent(10, getEventTitle(startTime), startTime, endTime);
event.setColor(getResources().getColor(R.color.red));
event.setName("match foot");
events.add(event);
startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 4);
startTime.set(Calendar.MINUTE, 20);
startTime.set(Calendar.MONTH, newMonth-1);
startTime.set(Calendar.YEAR, newYear);
endTime = (Calendar) startTime.clone();
endTime.set(Calendar.HOUR_OF_DAY, 5);
endTime.set(Calendar.MINUTE, 0);
event = new WeekViewEvent(10, getEventTitle(startTime), startTime, endTime);
event.setColor(getResources().getColor(R.color.green));
events.add(event);
startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 5);
startTime.set(Calendar.MINUTE, 30);
startTime.set(Calendar.MONTH, newMonth-1);
startTime.set(Calendar.YEAR, newYear);
endTime = (Calendar) startTime.clone();
endTime.add(Calendar.HOUR_OF_DAY, 2);
endTime.set(Calendar.MONTH, newMonth-1);
event = new WeekViewEvent(2, getEventTitle(startTime), startTime, endTime);
event.setColor(getResources().getColor(R.color.green));
events.add(event);
startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 5);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.MONTH, newMonth-1);
startTime.set(Calendar.YEAR, newYear);
startTime.add(Calendar.DATE, 1);
endTime = (Calendar) startTime.clone();
endTime.add(Calendar.HOUR_OF_DAY, 3);
endTime.set(Calendar.MONTH, newMonth - 1);
event = new WeekViewEvent(3, getEventTitle(startTime), startTime, endTime);
event.setColor(getResources().getColor(R.color.blue));
events.add(event);
startTime = Calendar.getInstance();
startTime.set(Calendar.DAY_OF_MONTH, 15);
startTime.set(Calendar.HOUR_OF_DAY, 3);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.MONTH, newMonth-1);
startTime.set(Calendar.YEAR, newYear);
endTime = (Calendar) startTime.clone();
endTime.add(Calendar.HOUR_OF_DAY, 3);
event = new WeekViewEvent(4, getEventTitle(startTime), startTime, endTime);
event.setColor(getResources().getColor(R.color.yellow));
events.add(event);
startTime = Calendar.getInstance();
startTime.set(Calendar.DAY_OF_MONTH, 1);
startTime.set(Calendar.HOUR_OF_DAY, 3);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.MONTH, newMonth-1);
startTime.set(Calendar.YEAR, newYear);
endTime = (Calendar) startTime.clone();
endTime.add(Calendar.HOUR_OF_DAY, 3);
event = new WeekViewEvent(5, getEventTitle(startTime), startTime, endTime);
event.setColor(getResources().getColor(R.color.red));
events.add(event);
startTime = Calendar.getInstance();
startTime.set(Calendar.DAY_OF_MONTH, startTime.getActualMaximum(Calendar.DAY_OF_MONTH));
startTime.set(Calendar.HOUR_OF_DAY, 15);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.MONTH, newMonth-1);
startTime.set(Calendar.YEAR, newYear);
endTime = (Calendar) startTime.clone();
endTime.add(Calendar.HOUR_OF_DAY, 3);
event = new WeekViewEvent(5, getEventTitle(startTime), startTime, endTime);
event.setColor(getResources().getColor(R.color.green));
events.add(event);
return events;
}
protected String getEventTitle(Calendar time) {
return String.format("Event of %02d:%02d %s/%d", time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), time.get(Calendar.MONTH)+1, time.get(Calendar.DAY_OF_MONTH));
}
private void setupDateTimeInterpreter(final boolean shortDate) {
mWeekView.setDateTimeInterpreter(new DateTimeInterpreter() {
@Override
public String interpretDate(Calendar date) {
SimpleDateFormat weekdayNameFormat = new SimpleDateFormat("EEE", Locale.getDefault());
String weekday = weekdayNameFormat.format(date.getTime());
SimpleDateFormat format = new SimpleDateFormat(" M/d", Locale.getDefault());
// All android api level do not have a standard way of getting the first letter of
// the week day name. Hence we get the first char programmatically.
// Details: http://stackoverflow.com/questions/16959502/get-one-letter-abbreviation-of-week-day-of-a-date-in-java#answer-16959657
if (shortDate)
weekday = String.valueOf(weekday.charAt(0));
return weekday.toUpperCase() + format.format(date.getTime());
}
@Override
public String interpretTime(int hour) {
return hour > 11 ? (hour - 12) + " PM" : (hour == 0 ? "12 AM" : hour + " AM");
}
});
}
/**
* Get events that were added by tapping on empty view.
* @param year The year currently visible on the week view.
* @param month The month currently visible on the week view.
* @return The events of the given year and month.
*/
private ArrayList<WeekViewEvent> getNewEvents(int year, int month) {
// Get the starting point and ending point of the given month. We need this to find the
// events of the given month.
Calendar startOfMonth = Calendar.getInstance();
startOfMonth.set(Calendar.YEAR, year);
startOfMonth.set(Calendar.MONTH, month - 1);
startOfMonth.set(Calendar.DAY_OF_MONTH, 1);
startOfMonth.set(Calendar.HOUR_OF_DAY, 0);
startOfMonth.set(Calendar.MINUTE, 0);
startOfMonth.set(Calendar.SECOND, 0);
startOfMonth.set(Calendar.MILLISECOND, 0);
Calendar endOfMonth = (Calendar) startOfMonth.clone();
endOfMonth.set(Calendar.DAY_OF_MONTH, endOfMonth.getMaximum(Calendar.DAY_OF_MONTH));
endOfMonth.set(Calendar.HOUR_OF_DAY, 23);
endOfMonth.set(Calendar.MINUTE, 59);
endOfMonth.set(Calendar.SECOND, 59);
// Find the events that were added by tapping on empty view and that occurs in the given
// time frame.
ArrayList<WeekViewEvent> events = new ArrayList<WeekViewEvent>();
for (WeekViewEvent event : mNewEvents) {
if (event.getEndTime().getTimeInMillis() > startOfMonth.getTimeInMillis() &&
event.getStartTime().getTimeInMillis() < endOfMonth.getTimeInMillis()) {
events.add(event);
}
}
return events;
}
@Override
public void onEventClick(WeekViewEvent event, RectF eventRect) {
Toast.makeText(getActivity(), "Clicked " + event.getName(), Toast.LENGTH_SHORT).show();
e=event;
/*this.getmNewEvents().remove(e);
this.getmWeekView().notifyDatasetChanged();*/
/* eventName.setText("Match");
eventPlace.setText("parc");
eventSchedule.setText("20h-22h");*/
EventFragment eventFragment= new EventFragment();
eventFragment.show(getFragmentManager(), "EventFragment");
}
@Override
public void onEventLongPress(WeekViewEvent event, RectF eventRect) {
Toast.makeText(getActivity(), "Long pressed event: " + event.getName(), Toast.LENGTH_SHORT).show();
}
@Override
public void onEmptyViewLongPress(Calendar time) {
// Set the new event with duration one hour.
Calendar endTime = (Calendar) time.clone();
// nombre random pour l'id de l'évènement
Random r = new Random();
int i1 = r.nextInt(200);
endTime.add(Calendar.HOUR, 1);
// Create a new event.
e = new WeekViewEvent(i1, "New event", time, endTime);
// appelle la fonction qui lance le dialogView pour récupérer les informations sur l'event
e=addEvent(e);
}
public WeekViewEvent addEvent(WeekViewEvent e){
Calendar_dialog dialog= new Calendar_dialog();
dialog.show(getFragmentManager(), "Calendar_dialog");
return e;
}
public static WeekViewEvent getE() {
return e;
}
public static WeekView getmWeekView() {
return mWeekView;
}
public static ArrayList<WeekViewEvent> getmNewEvents() {
return mNewEvents;
}
public static void setmWeekView(WeekView mWeekView) {
Cal.mWeekView = mWeekView;
}
public static void setE(WeekViewEvent e) {
Cal.e = e;
}
public static void setmNewEvents(ArrayList<WeekViewEvent> mNewEvents) {
Cal.mNewEvents = mNewEvents;
}
public int getIndex() {
return index;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Thank you for reading me !
Guillaume