【问题标题】:Error:(36, 74) error: incompatible types: Fragment cannot be converted to Context错误:(36、74)错误:不兼容的类型:片段无法转换为上下文
【发布时间】:2017-12-14 20:20:33
【问题描述】:

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

日历片段:

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();
}
}

【问题讨论】:

  • 移除 setContentView(R.layout.fragment_calendar);并在需要上下文的地方调用 getActivity() ...!!
  • 如果遇到任何问题,请先了解 Fragment,然后再提问。在 Fragment 中设置视图的例子有很多。

标签: android android-fragments


【解决方案1】:

onCreate 方法中删除setcontentview,您已经在onCreateView 中设置了视图

【讨论】:

    【解决方案2】:

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

    1. 请致电 onCreateView 而不是 onCreate。删除onCreate()

    2. 调用 getActivity() 而不是 thisgetActivity() 返回此片段当前关联的 Activity

    CalendarEventDataSource 数据源 = 新 CalendarEventDataSource(getActivity());

    问题

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

    此代码用于 Activity 。你应该使用你的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

    【讨论】:

    • +一个,一点也不,你让我学会了“要么不写答案,要么只写最好的方式”
    【解决方案3】:

    我想你的错误是指CalendarEventDataSource dataSource = new CalendarEventDataSource(this);这一行

    尝试将this 更改为getActivity()getActivity().getApplicationContext()getActivity().getBaseContext()

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

    【讨论】:

    • 请看他的问题,setContentView(R.layout.fragment_calendar);在 onCreate 中,我们如何在片段的 onCreate 方法中设置ContentView。
    • 真的问我看问题吗? @Jack 你在问题或我的答案中看到了什么错误? onCreateView 一切都好,只需要去掉onCreate,你在想什么?
    • 如果您不知道,我无法在 onCreate 中获取 setContentView...但是这个 PersianBlue 不知道片段。在提问之前,你必须建议他学习片段。
    • @MKJParekh Sir getApplicationContext() 此处不适用。其片段
    • 我的意思是getActivity().getApplicationContext(),抱歉写得不好。!总之,更新了。 @IntelliJAmiya
    猜你喜欢
    • 2019-11-16
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多