0

why is the word findViewById red ? i made a tabbed activity and this is one of the 3 screens that you can switch to.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    View rootView = inflater.inflate(R.layout.thekairi78, container, false);
    return rootView;
}
F43nd1r
  • 7,690
  • 3
  • 24
  • 62
Quentin Mayer
  • 33
  • 1
  • 4

1 Answers1

2

findViewById is a method of View or Activity, it cannot be called directly in fragments.

Corrected code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.thekairi78, container, false);  
    AdView mAdView = (AdView) rootView.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    return rootView;  
}

Try to not only use this, but to understand why this change has to be made. It'll help you in further developing.

F43nd1r
  • 7,690
  • 3
  • 24
  • 62