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

后端 未结 3 608

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条回答
  • 2021-01-26 05:01

    I suppose your error is indicating the line CalendarEventDataSource dataSource = new CalendarEventDataSource(this);

    Try and change this with either getActivity() or getActivity().getApplicationContext() or getActivity().getBaseContext().

    Also no need for onCreate() as other have suggested.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-26 05:07

    remove setcontentview from onCreate method you already set view in onCreateView

    0 讨论(0)
提交回复
热议问题