简体   繁体   中英

error: cannot find symbol method getSupportActionBar(),findViewById(int),getApplicationContext()

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

}                  

}

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
    .
    .
    .
    }

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

Context c = getContext().getApplicationContext();

Same goes for getSupportActionBar();

我认为,您应该给视图充气,并使用view.findViewByID(R.id.id) ,然后在方法末尾调用return。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM