alamkanak / Android-Week-View

Android Week View is an android library to display calendars (week view or day view) within the app. It supports custom styling.
Apache License 2.0
3.42k stars 1.23k forks source link

WeekView in Fragment: You must provide a MonthChangeListener #359

Closed JHthe4 closed 8 years ago

JHthe4 commented 8 years ago

Hi. I am trying to implement an AndroidWeekView in a fragment (in which it is the only view), but the app crashes with the error java.lang.IllegalStateException: You must provide a MonthChangeListener, although I have provided one in onCreateView(). The same code worked flawlessly in an activitiy's onCreate(), so I assume there to be an issue with the implementation of the fragment. See my code (shortened to the essentials for readability) below for details. Thanks for any help!

public class TimetableFragment extends Fragment{
private WeekView mWeekView;
public static TimetableFragment newInstance(String param1, String param2) {
        TimetableFragment fragment = new TimetableFragment();
        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);
        }
    }
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View mView = inflater.inflate(R.layout.fragment_timetable, container, false);
        mWeekView = (WeekView) mView.findViewById(R.id.weekView);
        setupWeekview();
        //return view
        return mView;
    }
private void setupWeekview(){
        if (mWeekView != null) {
            //set listener for month change
            mWeekView.setMonthChangeListener(new MonthLoader.MonthChangeListener() {
                @Override
                public List<? extends WeekViewEvent> onMonthChange(int i, int i1) {
                    return new ArrayList<WeekViewEvent>();
                    //TODO: Handle month change (return new list of events)
                }
            });

            //set listener for event click
            mWeekView.setOnEventClickListener(new WeekView.EventClickListener() {
                @Override
                public void onEventClick(WeekViewEvent event, RectF eventRect) {
                    //TODO: Handle event click
                }
            });

            //set event long press listener
            mWeekView.setEventLongPressListener(new WeekView.EventLongPressListener() {
                @Override
                public void onEventLongPress(WeekViewEvent event, RectF eventRect) {
                    //TODO: Handle event long press
                }
            });
}}
entropitor commented 8 years ago

I don't have any experience with fragments and Android-Week-View myself. But does this comment help? https://github.com/alamkanak/Android-Week-View/issues/300#issuecomment-229476623

JHthe4 commented 8 years ago

Upon further inspection and debugging, it appears as though there is some persistence issue with the variable mWeekViewLoader of the WeekView class. Passing the MonthChangeListener works and the constructor for WeekViewLoader is being called as well. However, in the method getMoreEvents(), mWeekViewLoader is once again null. Is there some other method resetting this field that is causing the bug?

entropitor commented 8 years ago

I don't think so..

In activities it works, so I have no idea why it would only reset it in fragments.

JHthe4 commented 8 years ago

Well, apparently now it works again. It turns out the problem wasn't an underlying bug in the library, or worse, android's definition of fragments: I forgot to remove the old weekView from my layout-v21 xml file. So, for anyone reading this in the future: triple check that you don't have a second weekView being loaded anywhere! @caske33, I'm so sorry for wasting your time!

entropitor commented 8 years ago

No problem. I'm glad it's resolved!