错误:(36,74)错误:不兼容的类型:片段无法转换为上下文

时间:2016-04-08 07:24:02

标签: android android-fragments

我有一个名为CalendarFragment的片段。我尝试执行程序时收到此错误。 错误:(36,74)错误:不兼容的类型:CalendarFragment无法转换为Context

CalendarFragment:

package app.pal.study.samplestudy;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.Date;
import java.util.List;

public class CalendarFragment  extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);

    return rootView;
}

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

private void refresh() {
    CalendarEventDataSource dataSource = new CalendarEventDataSource(this);
    dataSource.openReadOnlyDB();
    final List<CalendarEvent> calendarEvents = dataSource.getAllEvents();
    dataSource.close();

    CalAllEventsListAdapter adapter = new CalAllEventsListAdapter(calendarEvents);

    ListView listView = (ListView) getView().findViewById(R.id.all_event_list);
    listView.setAdapter(adapter);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        end();
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public void onBackPressed() {
    end();
}

private void end() {
    Intent data = new Intent();
    data.putExtra(Constants.DATE_KEY, (Date) getIntent().getExtras().get(Constants.DATE_KEY));
    setResult(RESULT_OK, data);
    finish();
}
}

3 个答案:

答案 0 :(得分:3)

从已在setcontentview

中设置视图的onCreate方法中删除onCreateView

答案 1 :(得分:2)

Fragment表示Activity中的行为或用户界面的一部分。

  1. 致电onCreateView而非onCreate。删除onCreate()

  2. 调用 getActivity()而不是 getActivity()返回此片段当前与之关联的活动

  3.   

    CalendarEventDataSource dataSource = new   CalendarEventDataSource(getActivity());

    问题

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

    此活动的代码。您应该使用onCreateView方法。

     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

答案 2 :(得分:1)

我认为您的错误表示行CalendarEventDataSource dataSource = new CalendarEventDataSource(this);

尝试使用thisgetActivity()getActivity().getApplicationContext()更改getActivity().getBaseContext()

也不需要像其他人所建议的那样onCreate()