Error:(36, 74) error: incompatible types: Fragment cannot be converted to Context

后端 未结 3 611

I have a fragment called CalendarFragment. I am getting this error when i tried executing the program. Error:(36, 74) error: incompatible types: CalendarFragment cannot be conve

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-26 05:02

    A Fragment represents a behavior or a portion of user interface in an Activity.

    1. Call onCreateView instead of onCreate. Remove onCreate()

    2. call getActivity() instead of this. getActivity() Return the Activity this fragment is currently associated with.

    CalendarEventDataSource dataSource = new CalendarEventDataSource(getActivity());

    Problem

        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_calendar);
    }
    

    This code for Activity . You should use your onCreateView method .

     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
      {
    
            View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
            return rootView;
      }
    

    http://developer.android.com/intl/es/guide/components/fragments.html

提交回复
热议问题