错误:找不到符号方法getSupportActionBar(),findViewById(int),getApplicationContext() [英] error: cannot find symbol method getSupportActionBar(),findViewById(int),getApplicationContext()

查看:86
本文介绍了错误:找不到符号方法getSupportActionBar(),findViewById(int),getApplicationContext()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的ScheduleFragment中有这段代码,我的目标是查看一个简单的日历查看器.但是不幸的是,它出现了标题中提到的错误.即使将公共类ScheduleFragment扩展Fragment"更改为公共类ScheduleFragment扩展AppCompatActivity",它也会给@Override错误.

So I have this code inside my ScheduleFragment in which my objective was to view a simple calendar viewer. But unfortunately, it gets an error mentioned in the title. Even if I change "public class ScheduleFragment extends Fragment" to "public class ScheduleFragment extends AppCompatActivity", it gives an error to @Override.

@TargetApi(Build.VERSION_CODES.N)
public class ScheduleFragment extends Fragment {

CompactCalendarView compactCalendar;
private SimpleDateFormat dateFormatMonth = new SimpleDateFormat("MMM - yyyy", Locale.getDefault());


public ScheduleFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_schedule, container, false);

    final ActionBar actionBar =     getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setTitle(null);

    compactCalendar = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
    compactCalendar.setUseThreeLetterAbbreviation(true);

    Event ev1 = new Event(Color.RED, 1477040400000L, "Teachers' Professional Day");
    compactCalendar.addEvent(ev1);

    compactCalendar.setListener(new CompactCalendarView.CompactCalendarViewListener() {
        @Override
        public void onDayClick(Date dateClicked) {
            Context context = getApplicationContext();
            if (dateClicked.toString().compareTo("Fri Oct 21 00:00:00 AST 2016") == 0) {
                Toast.makeText(context, "Teachers' Professional Day", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(context, "No Events Planned for that day", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onMonthScroll(Date firstDayOfNewMonth) {
            actionBar.setTitle(dateFormatMonth.format(firstDayOfNewMonth));
        }
    });

}                  

}

推荐答案

在片段中,您不能直接调用 findViewById(),而必须使用刚刚膨胀的视图并调用findViewById().
因此,您的onCreateView代码将是

In fragments, you can not directly call findViewById() and have to use the view that you just inflated and call findViewById() on it.
So your onCreateView code will be,

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_schedule, container, false);
    compactCalendar = (CompactCalendarView) v.findViewById(R.id.compactcalendar_view); 
   //Make sure that the view inflated above has these view elements which you are trying to initialize
    .
    .
    .
    }

类似地,对于 getApplicationContext(),您首先需要调用 getContext(),然后在其上调用 getApplicationContext()这样就变成了

Similarly for getApplicationContext(), you first need to call getContext() and then call getApplicationContext() on it So it becomes,

Context c = getContext().getApplicationContext();

getSupportActionBar();

这篇关于错误:找不到符号方法getSupportActionBar(),findViewById(int),getApplicationContext()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆