错误:找不到符号方法getsupportactionbar()、findviewbyid(int)、getapplicationcontext()

khbbv19g  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(297)

所以我在schedulefragment中有一段代码,我的目标是查看一个简单的日历查看器。但不幸的是,标题中提到了一个错误。即使我将“public class schedulefragment extends fragment”更改为“public class schedulefragment extends appcompatactivity”,它也会给@override带来一个错误。

  1. @TargetApi(Build.VERSION_CODES.N)
  2. public class ScheduleFragment extends Fragment {
  3. CompactCalendarView compactCalendar;
  4. private SimpleDateFormat dateFormatMonth = new SimpleDateFormat("MMM - yyyy", Locale.getDefault());
  5. public ScheduleFragment() {
  6. // Required empty public constructor
  7. }
  8. @Override
  9. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10. Bundle savedInstanceState) {
  11. // Inflate the layout for this fragment
  12. return inflater.inflate(R.layout.fragment_schedule, container, false);
  13. final ActionBar actionBar = getSupportActionBar();
  14. actionBar.setDisplayHomeAsUpEnabled(false);
  15. actionBar.setTitle(null);
  16. compactCalendar = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
  17. compactCalendar.setUseThreeLetterAbbreviation(true);
  18. Event ev1 = new Event(Color.RED, 1477040400000L, "Teachers' Professional Day");
  19. compactCalendar.addEvent(ev1);
  20. compactCalendar.setListener(new CompactCalendarView.CompactCalendarViewListener() {
  21. @Override
  22. public void onDayClick(Date dateClicked) {
  23. Context context = getApplicationContext();
  24. if (dateClicked.toString().compareTo("Fri Oct 21 00:00:00 AST 2016") == 0) {
  25. Toast.makeText(context, "Teachers' Professional Day", Toast.LENGTH_SHORT).show();
  26. }else {
  27. Toast.makeText(context, "No Events Planned for that day", Toast.LENGTH_SHORT).show();
  28. }
  29. }
  30. @Override
  31. public void onMonthScroll(Date firstDayOfNewMonth) {
  32. actionBar.setTitle(dateFormatMonth.format(firstDayOfNewMonth));
  33. }
  34. });
  35. }
  36. }
lvjbypge

lvjbypge1#

在片段中,不能直接调用 findViewById() 必须使用你刚才夸大的观点 findViewById() 在上面。
所以你的oncreateview代码是,

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

同样适用于 getApplicationContext() ,你首先需要打电话 getContext() 然后打电话 getApplicationContext() 所以它就变成了,

  1. Context c = getContext().getApplicationContext();

同样的道理 getSupportActionBar();

展开查看全部
6kkfgxo0

6kkfgxo02#

在我看来,应该膨胀视图并使用view.findviewbyid(r.id.id),然后在方法末尾调用return。

相关问题